An expression is not assigned, the coordinate attribute from the delegate of the MKAnnotation class

I made this diagram to better explain what my problems are.

enter image description here

So what can I do to fix this? Thanks =)

+4
source share
3 answers

CLLocationCoordinate2D is a struct , i.e. a value type. It is passed by value, which is another way of saying “copy”. If you assign your fields (for example, longitude), all that will do is change the copy; The original coordinate inside your Annotation will remain intact. This is why the property is not assigned.

To fix this, you must add separate properties for latitude and longitude and use them instead:

 @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 

Now your XML parser code can do this:

 if ([llave isEqualTo:@"lat"]) { puntoXML.latitude = [valor doubleValue]; } else if ([llave isEqualTo:@"lon"]) { puntoXML.longitude = [valor doubleValue]; } ... 
+3
source

Edit:

 puntoXML.coordinate.latitude = [valor floatValue]; 

in

 CLLocationCoordinate2D coord = puntoXML.coordinate; coord.latitude = [valor floatValue]; puntoXML.coordinate = coord; 

Make a similar change for longitude . Also note that you need to add curly braces in the if .

+4
source

The problem is that you are assigning a copy of CLLocationCoordinate2D your latitude / longitude.

puntoXML.coorinate returns a CLLocationCoordinate2D (copy), so assigning latitude will have no effect.

Instead, you need to create a full CLLocationCoordinate2D with a new latitude and longitude and set it at a time.

EDIT is better to provide separate properties for latitude / longitude and provide a custom set for each that sets their value in the coordinate instance variable.

+2
source

All Articles