How to get the current Location name as a string , when we know the latitude and longitude [duplicate]

喜你入骨 提交于 2019-12-04 18:11:05

Use this common function and just pass latitude and longitude as argument in this function.

-(NSString *)getAddressFromLatLon:(double)pdblLatitude withLongitude:(double)pdblLongitude
{
    NSString *urlString = [NSString stringWithFormat:kGeoCodingString,pdblLatitude, pdblLongitude];
    NSError* error;
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
    locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    return [locationString substringFromIndex:6];
}

and declare this

#define kGeoCodingString @"http://maps.google.com/maps/geo?q=%f,%f&output=csv"

This function will return address of the latitude and longitude you are passing.

Hope It helps.

Krishnabhadra

You are asking about Reverse Geocoding.. Use MKReverseGeocoding for this purpose.. This SO question explains it a bit..

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [self.locationManager stopUpdatingLocation];

    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        for (CLPlacemark * placemark in placemarks)
        {
            NSString *addressTxt = [NSString stringWithFormat:@"%@ %@,%@ %@", 
                                    [placemark subThoroughfare],[placemark thoroughfare],
                                    [placemark locality], [placemark administrativeArea]];
            NSLog(@"%@",addressTxt);
        }    
    }];
}

You can use the google API to get the address. Probably you have to send lat and long value as parameter.

Use google reverse geoCoding

sample http call here http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false

Maybe this is what you will have to use MKReverseGeocoder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!