我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;
}
不起作用?
答案 0 :(得分:0)
你的self.details.latitude / longitude值是否已经是真实/有效的地理坐标?
在这种情况下,您应该使用CLLocationCoordinate2DMake(lat,lon)
函数而不是MKCoordinateForMapPoint(MKMapPointMake())
这是错误的。