17

How can I get latitude and longitude from a full address (street, city, etc.) input by the user, using the iPhone SDK 3.x?

jscs
  • 62,161
  • 12
  • 145
  • 186
Luka
  • 1,421
  • 4
  • 19
  • 36

9 Answers9

30

Here's an updated, more compact, version of unforgiven's code, which uses the latest v3 API:

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
    double latitude = 0, longitude = 0;
    NSString *esc_addr =  [address 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;
    return center;
}

It makes the assumption that the coordinates for "location" come first, e.g. before those for "viewport", because it just takes the first coords it finds under the "lng" and "lat" keys. Feel free to use a proper JSON scanner (e.g. SBJSON) if you are worried about this simple scanning technique used here.

Thomas Tempelmann
  • 9,137
  • 6
  • 60
  • 120
  • 4
    This method works well. I found one bug, possibly because Google has changed the response format. scanUpToString and scanString should have another space before the :. It should look like: scanUpToString:@"\"lat\" :" and scanString:@"\"lat\" :" (both for lat and lng). – cberkley Jun 13 '12 at 21:01
  • @cberkley I made the change, but to be on the safe side, the scanner should be changed to not mind the spaces between the lat/lng and the colon. We never know when Google "fixes" this bad format again. In fact, the version by 'russes' might be the cleaner one for this. – Thomas Tempelmann Mar 26 '13 at 10:39
  • 1
    I posted my solution because the string scanner didn't seem to work back in 2011. Letting SBJson parse Google's response will make sense to beginners learning iOS coding from the Stanford CS193 online course. – russes Mar 29 '13 at 17:22
  • what if you input "test" in address parameter? – Gajendra K Chauhan Aug 14 '18 at 06:27
9

You can use Google Geocoding for this. It is as simple as getting data through HTTP and parsing it (it can return JSON KML, XML, CSV).

Valerii Hiora
  • 1,532
  • 13
  • 8
7

Here's a similar solution for obtaining the latitude and longitude from Google. Note: This example uses the SBJson library, which you can find on github:

+ (CLLocationCoordinate2D) geoCodeUsingAddress: (NSString *) address
{
    CLLocationCoordinate2D myLocation; 

// -- modified from the stackoverflow page - we use the SBJson parser instead of the string scanner --

        NSString       *esc_addr = [address stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
        NSString            *req = [NSString stringWithFormat: @"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
    NSDictionary *googleResponse = [[NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL] JSONValue];

    NSDictionary    *resultsDict = [googleResponse valueForKey:  @"results"];   // get the results dictionary
    NSDictionary   *geometryDict = [   resultsDict valueForKey: @"geometry"];   // geometry dictionary within the  results dictionary
    NSDictionary   *locationDict = [  geometryDict valueForKey: @"location"];   // location dictionary within the geometry dictionary

// -- you should be able to strip the latitude & longitude from google's location information (while understanding what the json parser returns) --

    DLog (@"-- returning latitude & longitude from google --");

    NSArray *latArray = [locationDict valueForKey: @"lat"]; NSString *latString = [latArray lastObject];     // (one element) array entries provided by the json parser
    NSArray *lngArray = [locationDict valueForKey: @"lng"]; NSString *lngString = [lngArray lastObject];     // (one element) array entries provided by the json parser

     myLocation.latitude = [latString doubleValue];     // the json parser uses NSArrays which don't support "doubleValue"
    myLocation.longitude = [lngString doubleValue];

    return myLocation;
}
russes
  • 1,052
  • 11
  • 15
4

Update version, using iOS JSON:

- (CLLocationCoordinate2D)getLocation:(NSString *)address {

    CLLocationCoordinate2D center;
    NSString *esc_addr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
    NSData *responseData = [[NSData alloc] initWithContentsOfURL:
                        [NSURL URLWithString:req]];    NSError *error;
    NSMutableDictionary *responseDictionary = [NSJSONSerialization
                                               JSONObjectWithData:responseData
                                               options:nil
                                               error:&error];
    if( error )
    {
        NSLog(@"%@", [error localizedDescription]);
        center.latitude = 0;
        center.longitude = 0;
        return center;
    }
    else {
        NSArray *results = (NSArray *) responseDictionary[@"results"];
        NSDictionary *firstItem = (NSDictionary *) [results objectAtIndex:0];
        NSDictionary *geometry = (NSDictionary *) [firstItem objectForKey:@"geometry"];
        NSDictionary *location = (NSDictionary *) [geometry objectForKey:@"location"];
        NSNumber *lat = (NSNumber *) [location objectForKey:@"lat"];
        NSNumber *lng = (NSNumber *) [location objectForKey:@"lng"];

        center.latitude = [lat doubleValue];
        center.longitude = [lng doubleValue];
        return center;
    }
}
mrd
  • 4,188
  • 8
  • 48
  • 84
3

The following method does what you asked for. You need to insert your Google maps key for this to work correctly.

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address{

    int code = -1;
    int accuracy = -1;
    float latitude = 0.0f;
    float longitude = 0.0f;
    CLLocationCoordinate2D center;

    // setup maps api key
    NSString * MAPS_API_KEY = @"YOUR GOOGLE MAPS KEY HERE";

    NSString *escaped_address =  [address stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    // Contact Google and make a geocoding request
    NSString *requestString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv&oe=utf8&key=%@&sensor=false&gl=it", escaped_address, MAPS_API_KEY];
    NSURL *url = [NSURL URLWithString:requestString];

    NSString *result = [NSString stringWithContentsOfURL: url encoding: NSUTF8StringEncoding error:NULL];
        if(result){
            // we got a result from the server, now parse it
            NSScanner *scanner = [NSScanner scannerWithString:result];
            [scanner scanInt:&code];
            if(code == 200){
                // everything went off smoothly
                [scanner scanString:@"," intoString:nil];
                [scanner scanInt:&accuracy];

                //NSLog(@"Accuracy: %d", accuracy);

                [scanner scanString:@"," intoString:nil];
                [scanner scanFloat:&latitude];
                [scanner scanString:@"," intoString:nil];
                [scanner scanFloat:&longitude];


                center.latitude = latitude;
                center.longitude = longitude;

                return center;


            }
            else{
                // the server answer was not the one we expected
                UIAlertView *alert = [[[UIAlertView alloc] 
                                       initWithTitle: @"Warning" 
                                       message:@"Connection to Google Maps failed"
                                       delegate:nil
                                       cancelButtonTitle:nil 
                                       otherButtonTitles:@"OK", nil] autorelease];

                [alert show];

                center.latitude = 0.0f;
                center.longitude = 0.0f;

                return center;


            }

        }
        else{
            // no result back from the server
            UIAlertView *alert = [[[UIAlertView alloc] 
                                   initWithTitle: @"Warning" 
                                   message:@"Connection to Google Maps failed"
                                   delegate:nil
                                   cancelButtonTitle:nil 
                                   otherButtonTitles:@"OK", nil] autorelease];

            [alert show];

            center.latitude = 0.0f;
            center.longitude = 0.0f;

            return center;
        }

    }

        center.latitude = 0.0f;
        center.longitude = 0.0f;

        return center;

}
Massimo Cafaro
  • 25,154
  • 14
  • 76
  • 92
1

There's also CoreGeoLocation, which wraps up the functionality in a framework (Mac) or static library (iPhone). Supports lookups through Google or Yahoo, if you have a preference for one over the other.

https://github.com/thekarladam/CoreGeoLocation

Cœur
  • 32,421
  • 21
  • 173
  • 232
Jablair
  • 4,916
  • 4
  • 26
  • 37
1

For the google map key solution, as described by unforgiven above, doesn't one has to make the app free? As per google terms & conditions: 9.1 Free, Public Accessibility to Your Maps API Implementation. Your Maps API Implementation must be generally accessible to users without charge.

With map kit in sdk 3.0 this is easily done using the SDK. See apple's manuals or follow: https://developer.apple.com/documentation/mapkit

Cœur
  • 32,421
  • 21
  • 173
  • 232
gammapoint
  • 11
  • 1
0
- (void)viewDidLoad
{
    app=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSLog(@"%@", app.str_address);


    NSLog(@"internet connect");

    NSString *Str_address=_txt_zipcode.text;

    double latitude1 = 0, longitude1 = 0;
    NSString *esc_addr =  [ Str_address 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:&latitude1];
            if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil])
            {
                [scanner scanDouble:&longitude1];
            }
        }
    }


    //in #.hfile
   // CLLocationCoordinate2D lat;
   // CLLocationCoordinate2D lon;
   // float address_latitude;
   // float address_longitude;


    lat.latitude=latitude1;
    lon.longitude=longitude1;

    address_latitude=lat.latitude;
    address_longitude=lon.longitude;

}
Paresh Hirpara
  • 485
  • 3
  • 10
0
func geoCodeUsingAddress(address: NSString) -> CLLocationCoordinate2D {
    var latitude: Double = 0
    var longitude: Double = 0
    let addressstr : NSString = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=\(address)" as NSString
    let urlStr  = addressstr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
    let searchURL: NSURL = NSURL(string: urlStr! as String)!
    do {
        let newdata = try Data(contentsOf: searchURL as URL)
        if let responseDictionary = try JSONSerialization.jsonObject(with: newdata, options: []) as? NSDictionary {
            print(responseDictionary)
            let array = responseDictionary.object(forKey: "results") as! NSArray
            let dic = array[0] as! NSDictionary
            let locationDic = (dic.object(forKey: "geometry") as! NSDictionary).object(forKey: "location") as! NSDictionary
            latitude = locationDic.object(forKey: "lat") as! Double
            longitude = locationDic.object(forKey: "lng") as! Double
        }} catch {
    }
    var center = CLLocationCoordinate2D()
    center.latitude = latitude
    center.longitude = longitude
    return center
}