calling coordinates from Core Data - ios

i've been trying to call the latitude and longitude of places that i have stored into the database. I've set the storage type as double.
At the the first view controller, i tried to push the coordinates, and in the log it works fine.
-(IBAction) mapsend:(id)sender
{
DiscoverAllMapViewController *fvController = [[DiscoverAllMapViewController alloc] initWithNibName:#"DiscoverAllMapViewController" bundle:nil];
[self.navigationController pushViewController:fvController animated:YES];
double dx = [wher.latx doubleValue];
double dy = [wher.longy doubleValue];
fvController.latx =[NSString stringWithFormat:#"%f",dx];
fvController.longy =[NSString stringWithFormat:#"%f",dy];
NSLog(#"checking latx :%#",fvController.latx);
[fvController release];
}
now at DisplayAllMapViewController, the log seems to always be null. I've tried setting the parsing instance to NSString, NSNumber, double, but it still doesnt send any value.
-(void)loadOurAnnotations
{
CLLocationCoordinate2D workingCoordinate;
MKCoordinateSpan span = {0.2, 0.2};
double xx = [latx doubleValue];
double yy = [longy doubleValue];
NSLog(#"check latx :%f",xx);
workingCoordinate.latitude = xx;
workingCoordinate.longitude = yy;
MKCoordinateRegion region = {workingCoordinate, span};
DiscoverAllMapAnnotation *appleStore1 = [[DiscoverAllMapAnnotation alloc] init];
[appleStore1 setCoordinate:workingCoordinate];
[appleStore1 setTitle:#"Kota Kinabalu"];
[appleStore1 setSubtitle:#"BOMB!!!!!"];
[appleStore1 setAnnotationType:DiscoverAllMapAnnotationType1];
mapView.showsUserLocation=TRUE;
[mapView addAnnotation:appleStore1];
[mapView setRegion:region];
[appleStore1 release];
}
please have a look.. thank you

The viewDidLoad of DiscoverAllMapViewController happens as soon as you do pushViewController which is before you've set the latx and longy properties.
Move the pushViewController call to after the properties are set (just before the fvController release).

Related

How to add new Annotation with existed annotations?

In my application, I was published recorded video from particular location. I can get the list of published location details from another service response. so Initially I got that published location details and displayed it on MapView. In MapView the pins are displayed with Cluster effect, so I used kingpin.
Here below I have loaded the Annotations on Map.
- (void)loadLocations:(NSArray *)arrayValues
{
_annotationArray = arrayValues;
[self.clusteringController setAnnotations:[self reSetannotations]];
[self.mapView setZoomEnabled:YES];
[self.mapView setCenterCoordinate:self.mapView.userLocation.coordinate];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow];
KPGridClusteringAlgorithm *algorithm = [KPGridClusteringAlgorithm new];
algorithm.annotationSize = CGSizeMake(25, 50);
algorithm.clusteringStrategy = KPGridClusteringAlgorithmStrategyTwoPhase;
self.clusteringController = [[KPClusteringController alloc] initWithMapView:self.mapView
clusteringAlgorithm:algorithm];
self.clusteringController.delegate = self;
self.clusteringController.animationOptions = UIViewAnimationOptionCurveEaseOut;
[self.clusteringController setAnnotations:[self annotations]];
NSString * lastobjlat;
double miles;
CLLocation * location = [COMMON currentLocation];
lastobjlat = [NSString stringWithFormat:#"%f",location.coordinate.latitude];
miles = 1.;
double scalingFactor = ABS( (cos(2 * M_PI * [lastobjlat floatValue] / 360.0) ));
MKCoordinateSpan span;
span.latitudeDelta = miles/69.0;
span.longitudeDelta = miles/(scalingFactor * 69.0);
MKCoordinateRegion region;
region.span = span;
region.center = location.coordinate;
[self.mapView setRegion:region animated:YES];
self.mapView.showsUserLocation = YES;
//Call in the below selectAnnotationAction method when I came to this, after I published new one on existed or new locations
if (COMMON.isRecentPublication == YES) {
COMMON.isRecentPublication = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self selectAnnotationAction];
});
}
}
//To reset the Annotations
- (NSArray *)reSetannotations
{
NSMutableArray *annotations = [NSMutableArray array];
return annotations;
}
//Here I managed location details on my custom marker class MapAnnotation.
- (NSArray *)annotations {
CLLocationCoordinate2D locationPort;
NSMutableArray *annotations = [NSMutableArray array];
NSString *latitude, *longitude;
if ([_annotationArray count] > 0) {
for (int i = 0; i <[_annotationArray count]; i++){
latitude = [NSString stringWithFormat:#"%#",[[_annotationArray objectAtIndex:i] valueForKey:#"latitude"]];
latitude = [latitude stringByReplacingOccurrencesOfString:#"\n" withString:#""];
latitude = [latitude stringByReplacingOccurrencesOfString:#"\t" withString:#""];
longitude = [NSString stringWithFormat:#"%#",[[_annotationArray objectAtIndex:i] valueForKey:#"longitude"]];
longitude = [longitude stringByReplacingOccurrencesOfString:#"\n" withString:#""];
longitude = [longitude stringByReplacingOccurrencesOfString:#"\t" withString:#""];
latitude = [NSString replaceEmptyStringInsteadOfNull:latitude];
longitude = [NSString replaceEmptyStringInsteadOfNull:longitude];
int publicationRatio = [[[_annotationArray objectAtIndex:i] valueForKey:#"publicationRatio"] intValue];
int publicationCount = [[[_annotationArray objectAtIndex:i] valueForKey:#"publicationsCount"] intValue];
int teazLocationId = [[[_annotationArray objectAtIndex:i] valueForKey:#"objectId"] intValue];
BOOL isUpgrade = [[[_annotationArray objectAtIndex:i] valueForKey:#"isUpgraded"] boolValue];
locationPort = CLLocationCoordinate2DMake([latitude doubleValue] ,
[longitude doubleValue]);
//TODO : This is my custom annotation method
MapAnnotation *a1 = [[MapAnnotation alloc] initWithCoordinate:locationPort
tag:i
publicationRatio:publicationRatio
publicationCount:publicationCount
teazLocationId:teazLocationId isUpgraded:isUpgrade];
a1.itemindex = i + 1;
a1.publicationRatio = publicationRatio;
a1.publicationCount = publicationCount;
a1.teazLocationId = teazLocationId;
a1.isUpgraded = isUpgrade;
a1.coordinate = CLLocationCoordinate2DMake([latitude doubleValue] ,
[longitude doubleValue] );
[annotations addObject:a1];
if (COMMON.isRecentPublication == YES) {
if ([COMMON.recentPublicationLocationID isEqual: #(publishedLocationId)]) {
_recentAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:a1 reuseIdentifier:#"cluster"];
}
}
}
}
return annotations;
}
It's not a problem to load Annotations with clustering effect on initial time. But when I published some one from same or different(new also) locations, I need to redirect Map screen(publishing section is other screen) and I need to display that publication detail with Active pin image.
Following steps I made to show the published location detail on Map
I created recentPublicationLocationID as a global variable and store the recentPublishedLocationID from response after published service.
Then this return type method - (NSArray *)annotations(after redirect to mapView I got the location details from another webservice after that it will called),
I have compared with recentPublishedId If existed or not. Then If existed, I have assigned my custom annotation (contains the recentPublished location details) to global MKPinAnnotationView instance - _recentAnnotationView
Then I directly called the didSelectPinAnnotation delegate method from this method - (void)loadLocations:(NSArray *)arrayValues like below,
//Pass recent published location details
if (COMMON.isRecentPublication == YES)
{
COMMON.isRecentPublication = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self selectAnnotationAction];
});
}
//selectAnnotationAction
- (void)selectAnnotationAction {
COMMON.isRecentPublication = NO;
COMMON.recentPublicationLocationID = nil;
[self mapView:self.mapView didSelectAnnotationView:_recentAnnotationView];
}
If I directly passed recentPublishedLocation details to didSelectAnnotationView delegate, I can only show the In Active pin instead of Active pin.
Then I debug with breakpoint why I can see the In active pin only ?
Because In this situation the didselect delegate was called and I can see the Active pin image. But it's only within sec.
Because viewForAnnotation delegate was called quickly for other pin annotations so the selected one goes to unselected state
This is the real problem. How can I overcome this work with clusters ?
Because when I displayed that published location detail correctly on map even it should be work with clustering effect. Yes I will zoom back to see the pins with cluster effect .
Finally I got the solution and achieved my requirement using ClusterKit library instead of Kingpin.
This tool really helpful me against possible to achieve append annotations and customize every thing adopt with my requirements.
So it's more helpful to me. Of course all of you.
And this tool supports Apple & Goole maps with Objective c as well as Swift.
I hope this one more helpful to other developers also.
Thanks :)

filter map annotations by distance

I need some help with a filter. How do I implement an algorithm that filters out related map annotations within the radius of user's location, thanks a lot guys! Any feedback is appreciated! Thank you and have a nice day.
My code I have so far:
CLLocationCoordinate2D coordinates;
CLLocationDistance test = 1000; // set distance
coordinates.latitude = [[row objectForKey:#"latitude"] doubleValue];// get user's latitude
coordinates.longitude = [[row objectForKey:#"longitude"] doubleValue];
MapAnnotation *annotation = [[MapAnnotation alloc] initWithCoordinate:coordinates title:titles subtitle:contents image:[row objectForKey:#"image"]]; // specify annotation's detail
[self.mapView addAnnotation:annotation];// adding annotation into the map
[annotation release];

iOS MapView only adding first annotation

I have an array named allposts that has 4 objects in it. When I run the loop below, only the first annotation is put onto the map. I print out the latitude and longitude of each "point" and they are indeed different, so Im not sure whats going on
for (PFObject *post in _allPosts)
{
NSLog(#"NEWANNOTATION");
PFGeoPoint *point = [post objectForKey:#"Location"];
NSLog(#"lat: %f long: %f", point.latitude, point.longitude);
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude);
CCBAnnotation *annotation = [[CCBAnnotation alloc] initWithCoordinate:coordinate];
[self.mapView addAnnotation:annotation];
}
You should also inspect mapView.annotations after doing this in order to determine if it's an adding problem or a display problem.

find locations (from mutablearray) close to users location ios

)
i have an mutable array of restaurantObjects serialized form json data, if current object is named restaurantDetail i can acces corrdinates like this
float x = [[restaurantDetail.coordinates objectAtIndex:0] floatValue];
float y = [[restaurantDetail.coordinates objectAtIndex:1] floatValue];
so i would like to extract restaurants which are close to users location and store them to array
and if user changes location array should get updated too
i would really apprichiate help because i am completely lost here
i have tried this form Muthub Ram
- (void) checkIfInRegion{
Restaurant * currentRestaurant;
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
for(int i=0;i<[restaurantsArray count];i++){
currentRestaurant = [restaurantsArray objectAtIndex:i];
//NSLog(#"%#",currentRestaurant.coordinates);
CLLocation * currentRestLoc = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake([[currentRestaurant.coordinates objectAtIndex:0] floatValue], [[currentRestaurant.coordinates objectAtIndex:1] floatValue])
altitude:0
horizontalAccuracy:0
verticalAccuracy:0
timestamp:[NSDate date]];
NSLog(#"%#",currentRestLoc);
if([locationManager.location distanceFromLocation:currentRestLoc] <100)
{
//if less than 100meters
//then add object in this array
[near_by_user addObject:[restaurantsArray objectAtIndex:i]];
}
}
NSLog(#"%#",locationManager.location);
NSLog(#"restaurants near%#",near_by_user);
[locationManager stopUpdatingLocation];
}
i get all coordinates form location but i dont get any objects in near_by_user array ( it remains empty)
http://pastebin.com/fc8q69Le here is log, last set of coordinates is my location same as one of restaurants
for(i=0;i<[array_res count];i++){
if([clloc distanceFromLocation:[array_res objectatindex:i]<100])
{
//if less than 100meters
//then add object in this array
[near_by_user addobject:[array_res objectatindex:i];
}
}
please add this code.if you have any clarification update here

calling [mapView addOverlay:] has no effect

For some reason, when I use [mapView addOverlay:], nothing happens.
Code:
NSData* dataLat = [[NSUserDefaults standardUserDefaults] objectForKey:#"latKey"];
NSArray* overlayLat = [NSKeyedUnarchiver unarchiveObjectWithData:dataLat];
double lats[overlayLat.count];
NSData* dataLong = [[NSUserDefaults standardUserDefaults] objectForKey:#"longKey"];
NSArray* overlayLong = [NSKeyedUnarchiver unarchiveObjectWithData:dataLong];
double longs[overlayLong.count];
for(int iii = 0; iii < overlayLat.count; iii++)
{
NSNumber* a = (NSNumber*)[overlayLat objectAtIndex:iii];
lats[iii] = [a doubleValue];
}
for(int iii = 0; iii < overlayLong.count; iii++)
{
NSNumber* a = (NSNumber*)[overlayLong objectAtIndex:iii];
longs[iii] = [a doubleValue];
}
int size = (sizeof(lats) / sizeof(lats[0]));
NSLog(#"%d", size);
MKMapPoint points[size];
for(int iii = 0; iii < overlayLong.count; iii++)
{
MKMapPoint point = MKMapPointMake(lats[iii], longs[iii]);
if(lats[iii] != 0)
points[iii] = point;
}
for(int iii = 0; iii < 15; iii++)
{
NSLog(#"Lat (x):%f", points[iii].x);
NSLog(#"Long (y):%f", points[iii].y);
}
MKPolyline* line = [MKPolyline polylineWithPoints:points count:size];
[mapView addOverlay:line];
NSLog(#"finished");
The vast majority of this code just accesses the data and turns it into usable coordinates. My question is (because I know that the coordinates are valid because of the NSLogs), why doesn't anything get added to the mapView when addOverlay: is called? I can post more code if need be. Thanks!
The polylineWithPoints:count: method takes a C array of MKMapPoint structs.
An MKMapPoint is not the same as a CLLocationCoordinate2D (latitude, longitude). You are creating MKMapPoint structs using MKMapPointMake but giving it latitude and longitude values. Such an MKMapPoint is not at the expected location.
Either use MKMapPointForCoordinate to create an MKMapPoint from lat/long coordinates or, probably easier, create a C array of CLLocationCoordinate2D structs and call polylineWithCoordinates:count: instead.
Could it be your 'size' is wrong? Your points array will also have missing items anywhere the source data had 0. I'd go with an NSMutableArray and add items to that if they are deemed valid, then count that at the end and pass it to polylineWithPoints.

Resources