Frage

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.
War es hilfreich?

Lösung

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];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top