Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get city name via CLlocationManager using latitude and longitude in IOS?

I am trying to get address via CLLocationManager using latitude and longitude but it only returns state and country name, I want city also, I am attaching my code can anybody tell me that how can I alter my code to get city name also.Below is my code

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    currentLocation = [locations objectAtIndex:0];
    CLGeocoder *geocoder1 = [[CLGeocoder alloc] init];

     NSLog(@"Detected Location : %f, %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);

    //CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:26.9260 longitude:75.8235];

    CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude];

    [geocoder1 reverseGeocodeLocation:newLocation
                   completionHandler:^(NSArray *placemarks, NSError *error) {

                       if (error) {
                           NSLog(@"Geocode failed with error: %@", error);
                           return;
                       }

                       if (placemarks && placemarks.count > 0)
                       {
                           CLPlacemark *placemark = placemarks[0];

                           NSDictionary *addressDictionary =
                           placemark.addressDictionary;

                           NSLog(@"%@ ", addressDictionary);
                           NSString *address = [addressDictionary
                                                objectForKey:(NSString *)kABPersonAddressStreetKey];
                           NSString *city = [addressDictionary
                                             objectForKey:(NSString *)kABPersonAddressCityKey];
                           NSString *state = [addressDictionary
                                              objectForKey:(NSString *)kABPersonAddressStateKey];
                           NSString *zip = [addressDictionary
                                            objectForKey:(NSString *)kABPersonAddressZIPKey];


                           NSLog(@"%@ %@ %@ %@", address,city, state, zip);
                       }

                   }];
}
like image 997
vijay Avatar asked Jul 22 '14 13:07

vijay


People also ask

How do you pull up latitude and longitude on iPhone?

Get GPS Coordinates in Maps on iPhone and iPad Tap the current location button on the top right. When the blue circle for your spot appears on the map, tap it. Swipe up from the bottom to view full details for your location and you'll see the Latitude and Longitude.

Can iPhone show GPS coordinates?

Touch and hold an area of the map that isn't labeled to drop a red pin. At the bottom, tap Dropped pin to find the coordinates.


1 Answers

Try this ..

-(NSString*)getAddressFromLatLong : (NSString *)latLng {
    //  NSString *string = [[Address.text stringByAppendingFormat:@"+%@",cityTxt.text] stringByAppendingFormat:@"+%@",addressText];
    NSString *esc_addr =  [latLng 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];
        NSMutableDictionary *data = [NSJSONSerialization JSONObjectWithData:[result dataUsingEncoding:NSUTF8StringEncoding]options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *dataArray = (NSMutableArray *)[data valueForKey:@"results" ];
        if (dataArray.count == 0) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please Enter a valid address" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
        }else{
            for (id firstTime in dataArray) {
                NSString *jsonStr1 = [firstTime valueForKey:@"formatted_address"];
                return jsonStr1;
            }
        }

    return nil;
}

Send comma separated Latitude longitude .. and try It's working fine

Reference Link Get Address From Latitude Longitude using Apple Function in iOS & iPad

like image 77
Jogendra.Com Avatar answered Nov 14 '22 05:11

Jogendra.Com