简体   繁体   English

CLLocationCoordinate2D到MKAnnotation

[英]CLLocationCoordinate2D to MKAnnotation

I have a MapView that I'm trying to add an Annotation at the current Coordinates. 我有一个MapView,我正在尝试在当前坐标处添加一个Annotation。

My Code in viewWillAppear: 我的代码在viewWillAppear中:

CLLocationCoordinate2D location;
location.latitude = [maps.barLat doubleValue];
location.longitude = [maps.barLong doubleValue];
[_mapView addAnnotation:location];

I'm getting an error on the addAnnotation that says 我在addAnnotation上遇到错误

Sending CLLocationCoordinate2D to parameter of incompatible type MKAnnotation. 将CLLocationCoordinate2D发送到不兼容类型MKAnnotation的参数。

All the other examples I see have no problem with this code, what am I missing? 我看到的所有其他示例对此代码没有任何问题,我缺少什么?

If you look at Apple's API docs , the addAnnotation: method takes an id<MKAnnotation> , not a CLLocationCoordinate2D . 如果查看Apple的API文档addAnnotation:方法将使用id<MKAnnotation> ,而不是CLLocationCoordinate2D That's the reason for your error. 这就是你错误的原因。

If you just want a simple annotation, without any fancy bells or whistles, just do this: 如果你只想要一个简单的注释,没有任何花哨的花哨或口哨,那就这样做:

CLLocationCoordinate2D location;
location.latitude = [maps.barLat doubleValue];
location.longitude = [maps.barLong doubleValue];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = location;
[_mapView addAnnotation: annotation];

Most people, however, wind up creating their own class that implements the MKAnnotation protocol, to provide custom annotations. 然而,大多数人最终创建了自己的类来实现MKAnnotation协议,以提供自定义注释。 But, if all you need is a pin, the above will work. 但是,如果您只需要一个引脚,上面的方法就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM