【问题标题】:ViewForAnnotation is not being called未调用 ViewForAnnotation
【发布时间】:2010-09-29 12:21:59
【问题描述】:

我正在尝试制作一张地图,在那里我可以看到我当前的位置,并查看街道的名称。

到目前为止,我可以在我的地图上放置一个图钉,但由于某种原因,我没有得到标注。 我在 viewForAnnotation 方法中放了一个 NSLog,但它没有被调用,所以我无法测试它。

有人可以帮我吗?

-(void)lat:(float)lat lon:(float)lon
{
    CLLocationCoordinate2D location;
    location.latitude = lat;
    location.longitude = lon;
    NSLog(@"Latitude: %f, Longitude: %f",location.latitude, location.longitude);
    //One location is obtained.. just zoom to that location

    MKCoordinateRegion region;
    region.center=location;
    //Set Zoom level using Span
    MKCoordinateSpan span;
    span.latitudeDelta=.005f;
    span.longitudeDelta=.005f;
    region.span=span;
    [map setRegion:region animated:TRUE];

    //MKReverseGeocoder *geocoder=[[MKReverseGeocoder alloc] initWithCoordinate:location];
    //geocoder.delegate=self;
    //[geocoder start];
    if (cPlacemark != nil) {
        [map removeAnnotation:cPlacemark];
    }
    cPlacemark=[[CustomPlacemark alloc] initWithCoordinate:location];
    cPlacemark.title = mPlacemark.thoroughfare;
    cPlacemark.subtitle = mPlacemark.locality;
    [map addAnnotation:cPlacemark];
    [cPlacemark release];

    [mLocationManager stopUpdatingLocation];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

// try to dequeue an existing pin view first
if ([annotation isKindOfClass:[CustomPlacemark class]]){
MKPinAnnotationView *pinView=(MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:@"customIdentifier"];

if (!pinView)
{
    // if an existing pin view was not available, create one
    MKPinAnnotationView* cPinAnnoView = [[[MKPinAnnotationView alloc]
                                           initWithAnnotation:annotation reuseIdentifier:@"customIdentifier"] autorelease];
    cPinAnnoView.pinColor = MKPinAnnotationColorPurple;
    cPinAnnoView.animatesDrop = YES;
    cPinAnnoView.canShowCallout = YES;
    // Add button
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [leftButton addTarget:self action:@selector(annotationViewClick:) forControlEvents:UIControlEventTouchUpInside];
    cPinAnnoView.leftCalloutAccessoryView = leftButton;

} else
    {
        pinView.annotation = annotation;
    }
    return pinView; 
}
return nil;

}

现在我已经将我的 viewForAnnotation 自定义为这样。 但是我仍然无法从我的别针中获得标注,并且别针仍然是红色的。 但它应该是紫色的,什么都没有

【问题讨论】:

  • 如果你的地图是从笔尖加载的,你记得把它连接起来吗?你记得设置委托吗?
  • 我的代表已“连接”到我的搜索栏和 searchdisplaycontroller,它现在正在工作:P 谢谢我仍然需要弄清楚为什么我没有得到标注
  • 您可以发布您的CustomPlacemark 课程吗?我看不出这不起作用的任何原因......
  • 我已经找到了我没有将代理设置为我的地图视图的答案

标签: objective-c


【解决方案1】:

我遇到了同样的问题,即没有将 MapView 委托设置为文件所有者。

  1. 打开你的笔尖
  2. 右键单击地图视图
  3. 将代理拖到文件的所有者处

【讨论】:

  • 或者如果使用故事板 - CTRL 单击 MapView 并拖动到橙色的小 ViewController 图标(在窗口下方的黑条上)
【解决方案2】:

正如你所说,我遇到了同样的问题。委托已设置为 ViewController,但未调用 viewForAnnotation 选择器。经过一番检查,我意识到如果你不在主线程中调用addAnotation,mapView 将不会调用viewForAnnotation,所以以下更新解决了我的问题:

之前:

[_mMapView addAnnotation:marker];

之后:

dispatch_async(dispatch_get_main_queue(), ^{
     [_mMapView addAnnotation:marker];
});

【讨论】:

  • 这对我也有帮助。奇怪的是它第一次没有工作,因为它在其他视图控制器中以“之前”的方式工作。 :)
  • @DevC 可能在您的代码在 UI 线程(又名主线程)中运行的其他地方。很高兴有帮助:-)
  • 我也遇到过这个问题(Swift)。我试图从来自 NSURLSession.dataTaskWithRequest() 的完成块中更新 UI。我必须从主线程执行我的 UI 更新。谢谢@Roozbeh dispatch_async(dispatch_get_main_queue()){self.addMyAnnotation()}
  • 非常感谢您的回答。我在 asyncDispatchQueue 中运行 webservice 查询,并在异步队列中向 mapview 添加注释,只是它没有显示。现在在 GetmainQueue 中添加了它。哇...Thanksssss ......我从早上开始就这个问题敲打。Upvoted。
【解决方案3】:

为了让viewForAnnotation被调用,添加mapView.delegate=self;到例如viewDidLoad 方法:

- (void)viewDidLoad {

    [super viewDidLoad];
    mapView.delegate=self;
}

【讨论】:

  • iOS 6 (openradar.appspot.com/12346693) 中存在一个错误 - 如果您在添加注释后设置委托,它们将获得默认的注释视图,而自定义的 mapView:viewForAnnotation 不会被调用。
【解决方案4】:

会不会是你的注解被添加到了MKMapView的当前视图区域之外?

【讨论】:

  • 我对此表示赞同,因为如果你认为你已经正确连接了所有东西,请仔细检查你的纬度和经度值:也许你把它们颠倒了,或者当它们应该是负数时,有一个(或两个)是正数.或者,在您的代码中,由于草率的剪切/粘贴工作,您将 lat 用于 lat 和 lon。 =)
【解决方案5】:

对于storyboard,Ctl将MKMapView拖到ViewController底栏的橙色圆圈处,选择delegate。

这样就可以解决问题了。

【讨论】:

    【解决方案6】:

    正如 cmets 中提到的 vatrif,您必须在 BEFORE 向您的 MKMapView 对象添加注释之前设置您的委托。

    【讨论】:

      【解决方案7】:

      其他人已经解释过了,很有可能您还没有将 mapview 委托连接到您的控制器。首先要检查

      【讨论】:

        【解决方案8】:

        我一直在使用 ios 9 Mapview 相关应用程序,我遇到了同样的问题。

        我以某种方式解决了我的问题,在我的情况下,我正在调整 mapview 的大小。 我在调整mapview 的大小后添加了delegate。它现在完美运行。!

        【讨论】:

          【解决方案9】:

          在为 mapview 设置委托后,如果仍然没有调用 viewforannotation,那么这是您错过的东西 - 将 self.mapView.showsUserLocation 设置为 YES,在界面构建器中,您可以在属性检查器中勾选显示 userLocation 选项.

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-03-07
            • 1970-01-01
            • 2016-07-15
            • 1970-01-01
            • 2012-06-05
            • 2011-11-20
            相关资源
            最近更新 更多