String to CLLocation latitude and Longitude
Asked Answered
E

5

20

I have two strings representing latitude and longitude like: "-56.6462520", and i want to assign then to a CLLocation object to compare to my current location.I tried the following code but i get errors only:

CLLocation * LocationAtual = [[CLLocation alloc]init];
LocationAtual.coordinate.latitude = @"-56.6462520";
LocationAtual.coordinate.longitude = @"-36.6462520";

and then compare the object with my actual location latitude and longitude. Any suggestions?

Extenuation answered 26/10, 2011 at 19:21 Comment(1)
Objective-C convention is camelCase for var names.Jadajadd
P
12

I think you need to:

LocationAtual.coordinate.latitude = [@"-56.6462520" floatValue];
LocationAtual.coordinate.longitude = [@"-36.6462520" floatValue];
Potshot answered 26/10, 2011 at 19:22 Comment(4)
I got the point, i think the conversion is correct, but im getting this error: "Lvaule required as left operand of assignment"...Extenuation
#5528378Potshot
I cannot delete an accepted answer, but agree AmitP's answer is correct. My answer was along the lines of "you can't use a string as a number"Potshot
Expression is not assignableCockneyism
D
109

You cannot assign directly into coordinate - it is a readonly property of CLLocation.
Use the following instance method:

- (instancetype)initWithLatitude:(CLLocationDegrees)latitude
                       longitude:(CLLocationDegrees)longitude

example:

CLLocation *LocationAtual = [[CLLocation alloc] initWithLatitude:-56.6462520 longitude:-36.6462520];
Doublebreasted answered 11/2, 2013 at 18:15 Comment(1)
That's true, you can't assign to CLLocation attributes.Vinegar
P
12

I think you need to:

LocationAtual.coordinate.latitude = [@"-56.6462520" floatValue];
LocationAtual.coordinate.longitude = [@"-36.6462520" floatValue];
Potshot answered 26/10, 2011 at 19:22 Comment(4)
I got the point, i think the conversion is correct, but im getting this error: "Lvaule required as left operand of assignment"...Extenuation
#5528378Potshot
I cannot delete an accepted answer, but agree AmitP's answer is correct. My answer was along the lines of "you can't use a string as a number"Potshot
Expression is not assignableCockneyism
R
9

Swift Answer for the lazy:

var LocationAtual: CLLocation = CLLocation(latitude: -56.6462520, longitude: -36.6462520)
Roybal answered 1/2, 2016 at 19:26 Comment(0)
B
1

CLLocation coordinate is actually a read only value

    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

So the best way to assign dummy data to coordinates is AMITp way

Betweenwhiles answered 10/1, 2014 at 9:51 Comment(0)
T
1

lattitude and longitude are double values so need to be assigned this way.

CLLocation *LocationAtual=[[CLLocation alloc] initWithLatitude:[[location objectForKey:@"latitude"] doubleValue] longitude:[[location objectForKey:@"longitude"] doubleValue]]
Thrawn answered 13/1, 2014 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.