我试图在地图视图上放置多个图钉但出现错误

Posted

技术标签:

【中文标题】我试图在地图视图上放置多个图钉但出现错误【英文标题】:I am trying to drop multiple pins on a map view but getting error 【发布时间】:2012-06-27 07:26:27 【问题描述】:
for (int i = 0; i < self.businessArray.count; i++) 
        Business *business = [self.businessArray objectAtIndex:i];
        MapAnnotation *mapAnnotation = [[MapAnnotation alloc] init]; 
        NSLog(@"%f %f", business.coordinate.latitude, business.coordinate.longitude);
        mapAnnotation.title = business.name;
        mapAnnotation.subtitle = business.address1;
        mapAnnotation.coordinate = business.coordinate;
        [bMapView addAnnotation:mapAnnotation];
        NSLog(@"ti %@", mapAnnotation.title);
        NSLog(@"sub %@", mapAnnotation.subtitle);
        NSLog(@"coo %f %f", mapAnnotation.coordinate.latitude, mapAnnotation.coordinate.longitude);
    

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation

    MKPinAnnotationView *pinView;

    if (annotation != mapView.userLocation) 
        static NSString *defauleID = @"myLocation";
        //        pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:defauleID];
        if (pinView == nil) 
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier:defauleID];
        
        pinView.pinColor = MKPinAnnotationColorGreen;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;

        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
        [rightButton addTarget:self 
                        action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
        pinView.rightCalloutAccessoryView = rightButton;
    

    return pinView;

错误: MapAnnotation 类的实例 0x1cdd97b0 已被释放,而键值观察者仍向其注册。观察信息被泄露,甚至可能被错误地附加到其他对象上。在 NSKVODeallocateBreak 上设置断点以在调试器中停止。这是当前的观察信息: ( 上下文:0x0,属性:0x1cd97a30>

【问题讨论】:

【参考方案1】:

每次在循环内释放注解

    for (int i = 0; i < self.businessArray.count; i++) 
            Business *business = [self.businessArray objectAtIndex:i];
            MapAnnotation *mapAnnotation = [[MapAnnotation alloc] init]; 
            NSLog(@"%f %f", business.coordinate.latitude, business.coordinate.longitude);
            mapAnnotation.title = business.name;
            mapAnnotation.subtitle = business.address1;
            mapAnnotation.coordinate = business.coordinate;
            NSLog(@"ti %@", mapAnnotation.title);
            NSLog(@"sub %@", mapAnnotation.subtitle);
            NSLog(@"coo %f %f", mapAnnotation.coordinate.latitude, mapAnnotation.coordinate.longitude);
            [bMapView addAnnotation:mapAnnotation];
            [mapAnnotation release];
        

【讨论】:

我使用 ARC。但是,我使用 [bMapView mapAnnotations: arrayOfAnnotations]; 解决了它【参考方案2】:

我认为您已通过添加观察者收到通知.. 当您收到该通知并执行与该观察者相关联的函数时释放您的 MapAnnotation 但您没有删除该观察者请在 dealloc 中删除观察者...

 [[NSNotificationCenter defaultSenter] removeObserver:self];

希望对你有帮助..

【讨论】:

以上是关于我试图在地图视图上放置多个图钉但出现错误的主要内容,如果未能解决你的问题,请参考以下文章

用户添加放置图钉,谷歌地图

如何在离线地图图块中放置多个图钉并获得经纬度?

如何在地图视图 iphone 或 ipad 上获得可移动的图钉?

iOS 谷歌地图在地图上添加多个图钉

弹出视图后出现错误的 UIToolbar

使用 MapKit 在 IOS 地图上放置图钉? [关闭]