Longitude and Latitude of Given address.
Sometimes we have to find the longitude and latitude of given address like ex (Delhi ,Rohini, street address 12 etc) for this we have to make some request using URL and pass the value with some space at the time of the request. so it will give the corresponding longitude and latitude value.
Ex: chennai
http://maps.google.com/maps/api/geocode/json?sensor=false&address=chennai
Ex : Delhi , 108186
http://maps.google.com/maps/api/geocode/json?sensor=false&address=delhi 108186
1 |
NSString *address = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@ %@ %@", self.streetField.text, self.cityField.text, self.countryField.text]; |
Here I have attached code snippet :
1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
-(CLLocationCoordinate2D) getLocationFromAddressString: (NSString*) addressStr { double latitude = 0, longitude = 0; NSString *esc_addr = [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr]; 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]; } } } CLLocationCoordinate2D center; center.latitude=latitude; center.longitude = longitude; float lat = center.latitude; float lon = center.longitude; NSLog(@"View Controller get Location Logitute : %f",center.latitude); NSLog(@"View Controller get Location Latitute : %f",center.longitude); return center; } |
2: Now with the help of Longitude and Latitude you can draw google Map and show the actual location.