Iphone 将MKUserLocation类强制转换为MKAnnotation协议
Iphone 将MKUserLocation类强制转换为MKAnnotation协议,iphone,ios,mkannotation,Iphone,Ios,Mkannotation,mapView:viewForAnnotation:方法有一个名为annotation(MKUserLocation)的参数。在我的应用程序中,我想将此注释类型转换为MKAnnotation 我试过这个: -(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { MyAnnotation *myAnnotation = (MyAnnotat
mapView:viewForAnnotation:
方法有一个名为annotation
(MKUserLocation)的参数。在我的应用程序中,我想将此注释类型转换为MKAnnotation
我试过这个:
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MyAnnotation *myAnnotation = (MyAnnotation*)annotation;
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
MyAnnotation*MyAnnotation=(MyAnnotation*)注释;
}
这里MyAnnotation是一个采用MKAnnotation
协议的自定义类。问题是myAnnotation仍然是MKUserLocation类型的对象。我想要myAnnotation
作为MKAnnotation对象。这个怎么打字?请帮帮我。对于所有批注,无论其类型如何,都会调用viewForAnnotation
委托方法。这包括地图视图自己的用户位置蓝点注释,其类型为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...
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
如果(![annotation isKindOfClass:[MyAnnotation class]])
{
//返回零(即默认视图)
//如果批注不是MyAnnotation类型。。。
//这包括MKUserLocation。
返回零;
}
//如果执行到这一点,,
//您知道注释的类型是MyAnnotation
//好吧,把它当作我的注解。。。
MyAnnotation*MyAnnotation=(MyAnnotation*)注释;
//在此处创建并返回自定义MKAnnotationView。。。
}