我正在编写一个带有mapview的应用程序。
我通过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获得长/纬度(而不是相对于我目前看到的视图)。