Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression is not assignable, coordinate attribute from MKAnnotation class delegate

I did this schema in order to explain better what is my troubles.

enter image description here

So, what can I do to fix it? Thank you =)

like image 993
Alejandro L. Avatar asked Jan 14 '23 01:01

Alejandro L.


2 Answers

Change:

puntoXML.coordinate.latitude = [valor floatValue];

to:

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

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

like image 68
rmaddy Avatar answered Jan 19 '23 10:01

rmaddy


The CLLocationCoordinate2D is a struct, i.e. a value type. It is passed around by value, which is another way of saying "copying". If you assign its fields (e.g. longitude) all that would do is modifying a copy; the original coordinate inside your Annotation would remain intact. That is why the property is not assignable.

To fix this, you should 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];
} ...
like image 45
Sergey Kalinichenko Avatar answered Jan 19 '23 11:01

Sergey Kalinichenko