how to get location(coordinates) from city name in iphone sdk?

后端 未结 3 876
温柔的废话
温柔的废话 2021-02-03 11:39

friends,

as we have geocoder getfromlocation(locationname,maximumResults) function of google api in android.

i dont see such function in iphone sdk to obtain lat

3条回答
  •  星月不相逢
    2021-02-03 12:23

    Use the following method to find Coordinates of a location from city name:

    Source:(http://sickprogrammersarea.blogspot.in/2014/03/programmatically-find-co-ordinates-of.html)

    -(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;
    NSLog(@"View Controller get Location Logitute : %f",center.latitude);
    NSLog(@"View Controller get Location Latitute : %f",center.longitude);
    return center;
    
    }
    

    Hope it will be Helpful.

提交回复
热议问题