【问题标题】:ios map annotation appearing in wrong placeios地图注释出现在错误的地方
【发布时间】:2012-07-17 03:00:39
【问题描述】:

我正在使用 IOS SDK 5.1 并使用 MapKit 开发应用程序。从 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];
    }

}

}

还有我的 viewForAnnotation 方法。

- (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;

}

问题是注释出现在错误的位置。我目前从我的后端服务返回一个位置,该位置具有华盛顿特区白宫的经纬度。问题是注释出现在中国。

即使我将 lat long 硬编码为 38.8977 、 77.0366 (基本上不是从 json 响应中解析它),它也会出现在中国。

对我做错了什么有什么想法吗?

【问题讨论】:

    标签: ios5 mapkit mkannotation


    【解决方案1】:

    有趣的是,似乎所有其他位置都可以正常工作。也许在白宫丢针有一些安全限制?

    更新...安娜在下面是正确的! :)

    【讨论】:

    • 没有这样的限制。白宫位于 38.8977, -77.0366( 77 经度,即西而非东)。
    • 哎呀不能相信我错过了。谢谢!
    【解决方案2】:

    MapKit 中坐标的表示是错误的,但我确信它们在 CLLocation 坐标中是正确的

    【讨论】:

    • 这是评论,不是答案
    猜你喜欢
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    相关资源
    最近更新 更多