gpt4 book ai didi

iphone - 将 MKUserLocation 类类型转换为 MKAnnotation 协议(protocol)

转载 作者:行者123 更新时间:2023-11-29 03:35:06 26 4
gpt4 key购买 nike

mapView:viewForAnnotation: 方法有一个名为 annotation (MKUserLocation) 的参数。在我的应用程序中,我想将此 annotation 类型转换为 MKAnnotation。

我试过这个:

-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MyAnnotation *myAnnotation = (MyAnnotation*)annotation;
}

这里MyAnnotation是一个采用MKAnnotation协议(protocol)的自定义类。问题是 myAnnotation 仍然是 MKUserLocation 类型对象。我想要 myAnnotation 作为 MKAnnotation 对象。如何输入这个?请帮助我。

最佳答案

viewForAnnotation 委托(delegate)方法会为所有 注释调用,无论其类型如何。这包括 map View 自己的用户位置蓝点注释,其类型为 MKUserLocation

在转换注释或尝试将其视为自定义类之前,您需要检查当前注释是否属于您感兴趣(或不感兴趣)的类型。

例如:

-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{
if (! [annotation isKindOfClass:[MyAnnotation class]])
{
//return nil (ie. default view)
//if annotation is NOT of type MyAnnotation...
//this includes MKUserLocation.
return nil;
}


//If execution reached this point,
//you know annotation is of type MyAnnotation
//so ok to treat it like MyAnnotation...

MyAnnotation *myAnnotation = (MyAnnotation*)annotation;

//create and return custom MKAnnotationView here...
}

关于iphone - 将 MKUserLocation 类类型转换为 MKAnnotation 协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19273179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com