在符合MKAnnotation协议的同时动态创建CLLocationCoordinate2D

时间:2012-08-09 06:15:35

标签: ios mkmapview mkannotation objective-c-protocol

MyClass符合MKAnnotation协议。根据{{​​3}},class需要实现coordinate属性,该属性应返回CLLocationCoordinate2D实例。 我的第一个实现是这样的:

-(CLLocationCoordinate2D)coordinate{
   return MKCoordinateForMapPoint(MKMapPointMake(self.details.latitude, self.details.longitude));
}

但是这没有用:每当我致电[self.mapView addAnnotation:instanceOfMyClass];时,它只是没有被添加到annotations的{​​{1}}数组中!

所以,我的解决方案是在mapView中定义一个实例变量_coordinate,并实现这样的协议一致性:

MyClass

现在有效了。 所以,这里的问题是为什么需要一个实例变量并且在运行中创建-(CLLocationCoordinate2D)coordinate { _coordinate.latitude = self.details.latitude; _coordinate.longitude = self.details.longitude; return _coordinate; } 不起作用?

1 个答案:

答案 0 :(得分:0)

你的self.details.latitude / longitude值是否已经是真实/有效的地理坐标? 在这种情况下,您应该使用CLLocationCoordinate2DMake(lat,lon)函数而不是MKCoordinateForMapPoint(MKMapPointMake())这是错误的。