user3792517 user3792517 - 2 months ago 12x
iOS Question

Get latitude and longitude based on Address using Geocoder class in iOS

I got the current location based on the Longitude and Latitude values and then I also got multiple places on google map using Annotation. Now I want to get the longitude and latitude values based on the Address( i.e street,city and county).Need some guidance on how this can be achieved.

Till now, this is what I have tried:-

#import "ViewController.h"

@interface ViewController ()


@implementation ViewController
@synthesize streetField = _streetField, cityField = _cityField, countryField = _countryField, fetchCoordinatesButton = _fetchCoordinatesButton, nameLabel = _nameLabel, coordinatesLabel = _coordinatesLabel;
@synthesize geocoder = _geocoder;

- (void)viewDidLoad

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

- (IBAction)fetchCoordinates:(id)sender {
NSLog(@"Fetch Coordinates");
if (!self.geocoder) {
self.geocoder = [[CLGeocoder alloc] init];

NSString *address = [NSString stringWithFormat:@"%@ %@ %@", self.streetField.text, self.cityField.text, self.countryField.text];
NSLog(@"GET Addres%@",address);

self.fetchCoordinatesButton.enabled = NO;

[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Fetch Gecodingaddress");
if ([placemarks count] > 0) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];

NSLog(@"GET placemark%@",placemark);

CLLocation *location = placemark.location;

NSLog(@"GET location%@",location);

CLLocationCoordinate2D coordinate = location.coordinate;

self.coordinatesLabel.text = [NSString stringWithFormat:@"%f, %f", coordinate.latitude, coordinate.longitude];


if ([placemark.areasOfInterest count] > 0) {
NSString *areaOfInterest = [placemark.areasOfInterest objectAtIndex:0];
self.nameLabel.text = areaOfInterest;

self.fetchCoordinatesButton.enabled = YES;

- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
[textField resignFirstResponder];
return YES;

Above code is not working to give me the latitude and longitude.Need some help on what am I doing wrong here or if I am missing on something.

Thanks in Advance.



Before using this check with iOS8 updation


This is for getting lat and long based user area like street name,state name,country.

-(CLLocationCoordinate2D) getLocationFromAddressString: (NSString*) addressStr {
    double latitude = 0, longitude = 0;
    NSString *esc_addr =  [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *req = [NSString stringWithFormat:@"", 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.longitude = longitude;
    NSLog(@"View Controller get Location Logitute : %f",center.latitude);
    NSLog(@"View Controller get Location Latitute : %f",center.longitude);
    return center;


call the method like this in viewdidload method or somewhere according to your project

[self getLocationFromAddressString:@"chennai"];

just pass this in your browser

and you will have json format with lat and lon"your city name here"

 NSString *address = [NSString stringWithFormat:@" %@ %@", self.streetField.text, self.cityField.text, self.countryField.text];

the usage of this method....

CLLocationCoordinate2D center;
        center=[self getLocationFromAddressString:@"uthangarai"];
      double  latFrom=&center.latitude;
      double  lonFrom=&center.longitude;

  NSLog(@"View Controller get Location Logitute : %f",latFrom);
        NSLog(@"View Controller get Location Latitute : %f",lonFrom);