我做了这个架构,以便更好地解释我的麻烦是什么.
那么,我该怎么做才能解决它呢?谢谢=)
解决方法
CLLocationCoordinate2D是结构,即值类型.它是通过值传递的,这是另一种说“复制”的方式.如果您分配其字段(例如经度),那么所有操作都是修改副本;注释中的原始坐标将保持不变.这就是该财产不可转让的原因.
要解决此问题,您应该为纬度和经度添加单独的属性,然后使用它们:
@interface Annotation : NSObject<MKAnnotation> @property (readwrite) CLLocationDegrees latitude; @property (readwrite) CLLocationDegrees longitude; @property (nonatomic,assign) CLLocationCoordinate2D coordinate; ... @end @implementation Annotation -(CLLocationDegrees) latitude { return _coordinate.latitude; } -(void)setLatitude:(CLLocationDegrees)val { _coordinate.latitude = val; } -(CLLocationDegrees) longitude{ return _coordinate.longitude; } -(void)setLongitude:(CLLocationDegrees)val { _coordinate.longitude = val; } @end
现在您的XML解析器代码可以执行此操作:
if ([llave isEqualTo:@"lat"]) { puntoXML.latitude = [valor doubleValue]; } else if ([llave isEqualTo:@"lon"]) { puntoXML.longitude = [valor doubleValue]; } ...