0

I have latitude and longitude values as string and want to convert it to corelocation. My main intension is to calculate the distance between the user's current location (returned by the device) and the location returned from the server.

calculate distance using lat and long.. This post helped me to find distance between two locations.

Which of the following will be the best way?

  1. Should I convert string to core location and calculate distance or
  2. should i convert the device location to string and calculate distance.
Community
  • 1
  • 1
Tamil
  • 1,173
  • 1
  • 13
  • 35
  • 5
    How can you possibly calculate distance by converting to string? – Merlevede Mar 20 '14 at 14:33
  • @Merlevede I haven't dont it. But here is the [link](http://stackoverflow.com/a/27943/334091) – Tamil Mar 20 '14 at 17:16
  • stating the reason for down voting will help a lot. Also please do remember that not everyone is expert here and there might be some basic questions and the reason may be that the user is not able to search properly (use proper keywords) to get the result. – Tamil Mar 20 '14 at 17:21

1 Answers1

3

Create and object like so:

CLLocation *locA = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];

lat1 / long2 are of type CLLocationDegrees which is a typedef of a double, representing the point in degrees.

Convert your lat / long to doubles and pass them in, like so:

CLLocation *locA = [[CLLocation alloc] initWithLatitude:[@"" doubleValue] longitude:[@"" doubleValue] ];

The above code was in the example you linked to, you could have very easily google'd the object and it would have brought you to the doc online.

EDIT:

As per Volker's suggestion, if you are getting numbers from a server, there is a possibility of localisation issues, where some local's use a decimal: 46.000 and others use a comma 46,000.

Something like this would be better:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSNumber *temp = [formatter numberFromString:@""];
[temp doubleValue];
Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
  • 1
    take care if the strings contain localized numbers using for example "," as decimal separator. It would be better to use number formatter to get the double values. – Volker Mar 20 '14 at 14:59
  • 1
    @Volker totally right, completely slipped my mind. Edited answer, I believe thats the correct code to do so ? – Simon McLoughlin Mar 20 '14 at 15:07