我正在编写一个内部带有地图视图的应用程序。
我通过 HTTP 请求从我的服务器下载一些注释的坐标。我应该解码 JsonResponse 并将注释添加到我的地图中。
摘自我的 JSON 响应
...{
"id": 1,
"lat": 20.34226,
"long": 19.988363
},..
我在这个方法中添加了我的注释,当上面的 JSON 下载完成时会调用这个方法。(我有一个自定义注释类。)
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading");
NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);
// convert to JSON
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
// show all values
for(NSDictionary *dict in res) {
CGFloat a = [[dict objectForKey:@"lat"] floatValue];
CGFloat b = [[dict objectForKey:@"long"] floatValue];
// CLLocationCoordinate2D location;
// location.latitude = a;
// location.longitude = b;
//
CLLocationCoordinate2D touchMapCoordinate
= [self.mapView convertPoint:CGPointMake(b,a) toCoordinateFromView:self.mapView];
MapViewAnnotation *myPin = [[MapViewAnnotation alloc] initWithCoordinate:touchMapCoordinate]; // Or whatever coordinates...
[self.mapView addAnnotation:myPin];
}
}
添加注释时,它们是相对于我的屏幕边界添加的,而不是整个地图视图。
我确实知道方法调用[self.mapView convertPoint:CGPointMake(a,b) toCoordinateFromView:self.mapView]
是造成这种情况的原因。
我想执行现在在注释中的代码,并跳过
CLLocationCoordinate2D touchMapCoordinate
= [self.mapView convertPoint:CGPointMake(b,a) toCoordinateFromView:self.mapView];
并直接前往MapViewAnnotation *myPin = [[MapViewAnnotation alloc] initWithCoordinate:location];
但是,当我这样做时,我收到以下错误:
An instance 0x858cc30 of class MapViewAnnotation was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0x1a000890> (
<NSKeyValueObservance 0x1a000850: Observer: 0xa117890, Key path: coordinate, Options: <New: NO, Old: NO, Prior: YES> Context: 0x0, Property: 0x1a0008d0>
)
我想知道的是:如何相对于我的地图添加这些注释,从 JSON 获得经度/纬度(而不是相对于我当前看到的视图)。