Reverse Geocoding using google maps api iOS

一世执手 提交于 2019-12-05 04:16:11

Pass your lattitude and longitude It will give you correct address...

[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude) completionHandler:^(GMSReverseGeocodeResponse* response, NSError* error) {
            NSLog(@"reverse geocoding results:");
           for(GMSAddress* addressObj in [response results])
            {
                NSLog(@"%@",[response results]);

                NSLog(@"coordinate.latitude=%f", addressObj.coordinate.latitude);
                NSLog(@"coordinate.longitude=%f", addressObj.coordinate.longitude);
                NSLog(@"thoroughfare=%@", addressObj.thoroughfare);
                NSLog(@"locality=%@", addressObj.locality);
                NSLog(@"subLocality=%@", addressObj.subLocality);
                NSLog(@"administrativeArea=%@", addressObj.administrativeArea);
                NSLog(@"postalCode=%@", addressObj.postalCode);
                NSLog(@"country=%@", addressObj.country);
                NSLog(@"lines=%@", addressObj.lines);
            }
        }];

Try changing this line

NSString *lookupString = [NSURL URLWithString:[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",latitude,longitude]];

with

NSString *lookupString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",latitude,longitude];

Provide valid latitude and longitude static and check it first. After you got success then only use dynamic latitude and longitude

-(void)getGoogleAdrressFromLatLong : (CGFloat)lat lon:(CGFloat)lon{
    //[self showLoadingView:@"Loading.."];
    NSError *error = nil;

    NSString *lookUpString  = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false", lat,lon];

    lookUpString = [lookUpString stringByReplacingOccurrencesOfString:@" " withString:@"+"];

    NSData *jsonResponse = [NSData dataWithContentsOfURL:[NSURL URLWithString:lookUpString]];

    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:&error];

   // NSLog(@"%@",jsonDict);

    NSArray* jsonResults = [jsonDict objectForKey:@"results"];

   // NSLog(@"%@",jsonResults);

}

Firstly, locationManager: didUpdateToLocation: fromLocation: is deprecated in iOS 6 and above. I would recommend you to replace this method with the new one: locationManager: didUpdateLocations: and to use GMSGeocoder reverseGeocodeCoordinate: completionHandler: for retrieving address from 2D-coordinate.

My solution based on using GMSGeocoder:

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.activityType = CLActivityTypeFitness;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.pausesLocationUpdatesAutomatically = NO;
[self.locationManager requestWhenInUseAuthorization];// iOS 8 requirement
[self.locationManager requestAlwaysAuthorization];// iOS 8 requirement

self.locateOnCurrentPosition = YES; // Flag for changing camera once you really needed
[self.locationManager startUpdatingLocation];

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    if (self.locateOnCurrentPosition) {
        NSLog(@"CLLocationManager didUpdateLocations: %@", [locations lastObject]);
        self.locateOnCurrentPosition = NO;
        CLLocation *location = [locations lastObject];
        self.mapView.camera = [GMSCameraPosition cameraWithTarget:location.coordinate zoom:16]; // GMSMapView entity
        self.mapMarker.position = self.mapView.camera.target; // GMSMarker entity
        [[GMSGeocoder geocoder] reverseGeocodeCoordinate:location.coordinate completionHandler:
        ^(GMSReverseGeocodeResponse *response, NSError *error){
            NSLog(@"Address: %@", response.firstResult);
        }];
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!