24

I want to get the location name from the coordinate value. Here is code,

- (void)viewWillAppear:(BOOL)animated {  
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 39.281516;
    zoomLocation.longitude= -76.580806;
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
    MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];                
    [_mapView setRegion:adjustedRegion animated:YES];      
}

So, From that Latitude and longitude , i want to know that location name.

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
Apple
  • 736
  • 1
  • 6
  • 24
  • Hello Friends, Thanks for your answer, i appreciate it, but i got the solution of this, i am not able to post the answer because don't have more than 10 reputation. I will post it after 8 Hours as per the rule of StackOverflow. I just used the google map API to get the Location string after passing the LAt & Long. – Apple Sep 26 '12 at 11:22

5 Answers5

81

The Below code shall work in ios5 and above

 CLGeocoder *ceo = [[CLGeocoder alloc]init];
 CLLocation *loc = [[CLLocation alloc]initWithLatitude:32.00 longitude:21.322]; //insert your coordinates

[ceo reverseGeocodeLocation:loc
          completionHandler:^(NSArray *placemarks, NSError *error) {
              CLPlacemark *placemark = [placemarks objectAtIndex:0];
              if (placemark) {


                  NSLog(@"placemark %@",placemark);
                  //String to hold address
                  NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                  NSLog(@"addressDictionary %@", placemark.addressDictionary);

                  NSLog(@"placemark %@",placemark.region);
                  NSLog(@"placemark %@",placemark.country);  // Give Country Name
                  NSLog(@"placemark %@",placemark.locality); // Extract the city name
                  NSLog(@"location %@",placemark.name);
                  NSLog(@"location %@",placemark.ocean);
                  NSLog(@"location %@",placemark.postalCode);
                  NSLog(@"location %@",placemark.subLocality);

                  NSLog(@"location %@",placemark.location);
                  //Print the location to console
                  NSLog(@"I am currently at %@",locatedAt);
              }
              else {
                  NSLog(@"Could not locate");
              }
          }
 ];
AppleDelegate
  • 4,269
  • 1
  • 20
  • 27
7
-(NSString *)getAddressFromLatLon:(double)pdblLatitude withLongitude:(double)pdblLongitude
{
  NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv",pdblLatitude, pdblLongitude];
  NSError* error;
  NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
   // NSLog(@"%@",locationString);

  locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
  return [locationString substringFromIndex:6];
}
Apple
  • 736
  • 1
  • 6
  • 24
  • This is a good answer, although it uses Google Maps ... I prefer the standard iOS way to get the location. – Frederic Adda May 25 '15 at 12:40
  • 3
    Now this API is not working for more information please go to this URL https://developers.google.com/maps/documentation/geocoding – nivritgupta Aug 21 '15 at 09:55
3

Use this method

CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 

[geocoder reverseGeocodeLocation: zoomLocation completionHandler:^(NSArray* placemarks, NSError* error){
}
 ];
Neo
  • 2,807
  • 1
  • 16
  • 18
1

In iOS 5.0 and later, you can use CLGeocoder of Core Location framework, as for iOS lower than 5.0, MKReverseGeocoder of Map Kit Framework. Good luck!

Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29
0

Here is block to get address string from current location

in .h file

typedef void(^addressCompletionBlock)(NSString *);

-(void)getAddressFromLocation:(CLLocation *)location complationBlock:(addressCompletionBlock)completionBlock;

in .m file
#pragma mark - Get Address
-(void)getAddressFromLocation:(CLLocation *)location complationBlock:(addressCompletionBlock)completionBlock
{
    //Example URL
    //NSString *urlString = @"http://maps.googleapis.com/maps/api/geocode/json?latlng=23.033915,72.524267&sensor=true_or_false";

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

    NSError* error;
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
    NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[locationString dataUsingEncoding:NSUTF8StringEncoding]
                                                          options:0 error:NULL];

    NSString *strFormatedAddress = [[[jsonObject valueForKey:@"results"] objectAtIndex:0] valueForKey:@"formatted_address"];
    completionBlock(strFormatedAddress);
}

and to call function

CLLocation* currentLocation = [[CLLocation alloc] initWithLatitude:[SharedObj.current_Lat floatValue] longitude:[SharedObj.current_Long floatValue]];

    [self getAddressFromLocation:currentLocation complationBlock:^(NSString * address) {
        if(address) {
            NSLog(@"Address:: %@",address);
        }
    }];
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81