Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
- (CLLocation*) addressToLocation:(NSString*) address{
CLLocation *location;
NSString *url = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true", address];
url = [url stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *wurl = [NSURL URLWithString:url];
NSData *data = [NSData dataWithContentsOfURL: wurl];
// Fail to get data from server
if (nil == data) {
NSLog(@"Error: Fail to get data");
}
else{
// Parse the json data
NSError *error;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error];
// Check status of result
NSString *resultStatus = [json valueForKey:@"status"];
// If responce is valid
if ( (nil == error) && [resultStatus isEqualToString:@"OK"] ) {
NSDictionary *locationDict=[json objectForKey:@"results"] ;
NSArray *temp_array=[locationDict valueForKey:@"geometry"];
NSArray *temp_array2=[temp_array valueForKey:@"location"];
NSEnumerator *enumerator = [temp_array2 objectEnumerator];
id object;
while ((object = [enumerator nextObject])) {
double latitude=[[object valueForKey:@"lat"] doubleValue];
double longitude=[[object valueForKey:@"lng"] doubleValue];
location=[[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
NSLog(@"CLLocation lat is %f -------------& long %f",location.coordinate.latitude, location.coordinate.longitude);
}
}
}
return location;
}
@kkdai

This comment has been minimized.

Copy link
Owner Author

commented Jul 8, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.