Objective c 未调用ViewForAnnotation

Objective c 未调用ViewForAnnotation,objective-c,Objective C,我正试图制作一张地图,在那里我可以看到我当前的位置,并看到这条街的名称 到目前为止,我能够在我的地图上放置一个pin,但由于某种原因,我没有得到标注。 我在viewForAnnotation方法中放了一个NSLog,但是它没有被调用,所以我无法测试它 有人能帮我吗 -(void)lat:(float)lat lon:(float)lon { CLLocationCoordinate2D location; location.latitude = lat; location

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

到目前为止,我能够在我的地图上放置一个pin,但由于某种原因,我没有得到标注。 我在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;
-(void)lat:(float)lat lon:(float)lon
{
CLLOCATION坐标2D定位;
位置纬度=纬度;
location.longitude=lon;
NSLog(@“纬度:%f,经度:%f”,位置。纬度,位置。经度);
//获得一个位置。只需缩放到该位置
协调区域;
区域。中心=位置;
//使用Span设置缩放级别
Mk坐标跨度;
span.latitudeDelta=.005f;
span.longitudeDelta=.005f;
地区span=span;
[地图设置区域:区域动画:真];
//MKReverseGeocoder*地理编码器=[[MKReverseGeocoder alloc]initWithCoordinate:location];
//geocoder.delegate=self;
//[地理编码器启动];
如果(cPlacemark!=nil){
[地图移除说明:cPlacemark];
}
cPlacemark=[[CustomPlacemark alloc]initWithCoordinate:location];
cPlacemark.title=mPlacemark.throughtree;
cPlacemark.subtitle=mPlacemark.locality;
[地图添加注释:cPlacemark];
[cPlacemark发布];
[mLocationManager停止更新位置];
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
if([annotation isKindOfClass:[MKUserLocation类]])
返回零;
//首先尝试将现有pin视图出列
if([annotation isKindOfClass:[CustomPlacemark class]]){
MKPinAnnotationView*pinView=(MKPinAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:@“customIdentifier”];
如果(!pinView)
{
//如果现有接点视图不可用,请创建一个接点视图
MKPinAnnotationView*cPinAnnoView=[[[MKPinAnnotationView alloc]
initWithAnnotation:注释重用标识符:@“customIdentifier”]自动释放];
cPinAnnoView.pinColor=MKPinAnnotationColorPurple;
cPinAnnoView.animatesDrop=是;
cPinAnnoView.canShowCallout=是;
//添加按钮
UIButton*leftButton=[UIButton Button类型:UIButtonTypedTailButton];
[leftButton添加目标:自我操作:@选择器(注释视图单击:)用于控制事件:UIControlEventTouchUpInside];
cPinAnnoView.leftCalloutAccessoryView=leftButton;
}否则
{
pinView.annotation=注释;
}
返回pinView;
}
返回零;
}

现在我已经自定义了我的viewForAnnotation,如下所示。 但我仍然无法从我的pin中获取调用,pin仍然是红色的。
但是它应该是紫色的,一无所有

可能是您的注释已添加到MKMapView的当前视图区域之外吗?

为了获得要调用的
视图注释
,请添加
mapView.delegate=self到,例如
viewDidLoad
方法:

- (void)viewDidLoad {

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

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

  • 打开你的笔尖
  • 在地图视图上单击鼠标右键
  • 将代理拖动到文件的所有者

  • 其他人已经解释过,您没有将mapview代理连接到控制器的可能性很高。这是检查的第一件事,正如你提到的,我也有同样的问题。委托已设置为ViewController,但未调用
    viewForAnnotation
    选择器。经过一些检查后,我意识到如果不在主线程中调用
    addAnotation
    ,mapView将不会调用
    viewfornotation
    ,因此以下更新解决了我的问题:

    之前:

    [_mMapView addAnnotation:marker];
    
    之后:

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

    对于情节提要,Ctl将MKMapView拖动到ViewController底部栏上的橙色圆圈,然后选择“委派”


    这将解决问题。

    正如vatrif在评论中提到的,在向MKMapView对象添加注释之前,您必须设置您的代理。

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

    不知何故,我解决了我的问题,在我的例子中,我正在调整
    地图视图的大小。
    
    在调整
    地图视图的大小后,我添加了
    委托
    。它现在工作得很好

    为地图视图设置代理后,如果仍然没有调用viewforannotation,则这是您遗漏的内容-将self.mapview.showsUserLocation设置为YES,在interface builder中,您可以勾选属性检查器中的shows userLocation选项。

    如果您的地图是从nib加载的,你记得把它接线好吗?你记得设置代理吗?我的代理已“连接”到我的搜索栏和searchdisplaycontroller。它现在正在工作:P谢谢,我仍然需要弄清楚为什么我没有获得调用,尽管你能发布你的
    CustomPlacemark
    类吗?我看不出这不起作用的任何原因…我已经找到了答案,我没有将代理设置为我的MapView,或者如果使用情节串联板-CTRL单击MapView并拖动到小橙色ViewController图标(在窗口正下方的黑条上)iOS 6中有一个bug()-如果在添加批注后设置代理,则它们将获得默认批注视图,并且不会调用自定义mapView:viewForAnnotation。我对此进行了改进,因为如果您认为所有内容都连接正确,请仔细检查纬度和经度值:可能您将其反转,或者同时使用一个(或两个)当他们应该是消极的时候,要积极。或者,在代码中,由于剪切/粘贴作业马虎,您对lat和lon都使用lat这也帮助了我。奇怪的是,它第一次没有工作,因为它在其他ViewController中以“之前”的方式工作。:)@DevC可能在其他地方,您的代码是在UI线程(又称主线程)中运行的。很高兴这有帮助:-)我也有这个问题(斯威夫特)。我正在尝试从一个完成更新UI