我有一个 MapView,我正在尝试在当前坐标处添加注释。
我在 viewWillAppear 中的代码:
CLLocationCoordinate2D location;
location.latitude = [maps.barLat doubleValue];
location.longitude = [maps.barLong doubleValue];
[_mapView addAnnotation:location];
我在 addAnnotation
上遇到错误,内容为
Sending CLLocationCoordinate2D to parameter of incompatible type MKAnnotation.
我看到的所有其他示例都没有问题,我缺少什么?
最佳答案
如果你看Apple's API docs ,addAnnotation:
方法需要 id<MKAnnotation>
,不是CLLocationCoordinate2D
。这就是你的错误的原因。
如果您只想要一个简单的注释,没有任何花哨的花哨的东西,只需这样做:
CLLocationCoordinate2D location;
location.latitude = [maps.barLat doubleValue];
location.longitude = [maps.barLong doubleValue];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = location;
[_mapView addAnnotation: annotation];
然而,大多数人最终都会创建自己的类来实现 MKAnnotation
协议(protocol),提供自定义注释。但是,如果您只需要一个引脚,上面的方法就可以了。
关于iphone - CLLocationCooperative2D 到 MKAnnotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11571630/