Получить широту / долготу от адреса - PullRequest
17 голосов
/ 01 октября 2009

Как узнать широту и долготу по полному адресу (улица, город и т. Д.), Введенному пользователем с помощью iPhone SDK 3.x?

Ответы [ 9 ]

30 голосов
/ 09 июня 2010

Вот обновленная, более компактная, версия непрощенного кода, в которой используется последняя версия API v3:

- (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;
}

Предполагается, что координаты "местоположения" идут первыми, например, перед теми для "viewport", потому что он просто берет первые координаты, которые он находит под ключами "lng" и "lat". Не стесняйтесь использовать надлежащий сканер JSON (например, SBJSON), если вы беспокоитесь об этой простой технике сканирования, используемой здесь.

9 голосов
/ 01 октября 2009

Вы можете использовать Google Geocoding для этого . Это так же просто, как получить данные через HTTP и проанализировать их (это может вернуть JSON KML, XML, CSV).

7 голосов
/ 22 октября 2011

Вот аналогичное решение для получения широты и долготы от Google. Примечание. В этом примере используется библиотека SBJson, которую вы можете найти на 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;
}
4 голосов
/ 08 марта 2014

Обновление версии с использованием 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;
    }
}
3 голосов
/ 01 октября 2009

Следующий метод делает то, что вы просили. Вам нужно вставить свой ключ Google Maps для того, чтобы это работало правильно.

- (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;

}
1 голос
/ 04 марта 2010

Для решения ключа карты Google, как описано выше, разве не нужно делать приложение бесплатным? В соответствии с положениями и условиями Google: 9.1. Бесплатный общедоступный доступ к реализации API ваших карт. Ваша реализация API Карт должна быть общедоступной для пользователей бесплатно.

С помощью комплекта карт в SDK 3.0 это легко сделать с помощью SDK. См. Руководства Apple или следуйте: https://developer.apple.com/documentation/mapkit

1 голос
/ 01 октября 2009

Существует также CoreGeoLocation, который объединяет функциональность в каркас (Mac) или статическую библиотеку (iPhone). Поддерживает поиск через Google или Yahoo, если вы предпочитаете один другому.

https://github.com/thekarladam/CoreGeoLocation

0 голосов
/ 31 марта 2017
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
}
0 голосов
/ 10 декабря 2014
- (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;

}
...