Reputation: 629
Im developing an app with IOS SDK 5.1 and using MapKit. I have the following code to place an annotation on map after retrieval of data from an api.
- (void)placeAnnotationsOnMap:(NSString *)responseString {
for (id<MKAnnotation> annotation in _mapView.annotations) {
[_mapView removeAnnotation:annotation];
}
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSError *e = nil;
NSArray *jsonArray =
[NSJSONSerialization
JSONObjectWithData: jsonData
options: kNilOptions
error: &e];
for(NSArray *cache in jsonArray ){
for(NSDictionary *item in jsonArray) {
NSLog(@"Item: %@", item);
NSString * comment = [item objectForKey:@"comment"];
NSNumber * latitude = [item objectForKey:@"locationLat"];
NSNumber * longitude = [item objectForKey:@"locationLong"];
NSString * numCachers = [item objectForKey:@"numCachers"];
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
CustomAnnotation *cacheAnnotation = [[CustomAnnotation alloc] initWithTitle:comment numCachers:numCachers coordinate:coordinate];
[_mapView addAnnotation:cacheAnnotation];
}
}
}
And my viewForAnnotation method.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"CustomAnnotation";
CLLocationCoordinate2D coord = [annotation coordinate];
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
return annotationView;
}
The problem is the annotation appears in the wrong place. I currently return one location from my backend service that has the latitude longitude of the white house in washington dc. The problem is that the annotation shows up in china.
Even if I hard code the lat long to 38.8977 , 77.0366 ( basically not parsing it from the json response ) it shows up in China.
Any thoughts on what Im doing wrong?
Upvotes: 1
Views: 737
Reputation: 1
The representation of coordinates are wrong in MapKit, but i am sure they are correct in CLLocation coordinates
Upvotes: 0
Reputation: 629
Interestingly, it seem all other locations work fine. Perhaps there is some security restriction on dropping pins on the white house?
Update...Anna is correct below! :)
Upvotes: 1