Hello Friends,
In order to fetch the latitude and longitude of a location based on its address,you may use the following code:
double latitude = 0;
double longitude = 0;
Use the google api along with your address
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", @"Your address "];
Connect to the api and the result is stored here in result variable
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
Creating location coordinate on the basis of the fetched latitude and longitude
CLLocationCoordinate2D center;
center.latitude=latitude;
center.longitude = longitude;
0 Comment(s)