3

Currently i am working in iPhone application, Using CLLocationManager to get Latitude and Longitude values fine. I didn't know this? How to get the device location (Address) name from this latitude and longitude value? please help me

Thanks in Advance

I tried this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    LocationManager = [[CLLocationManager alloc]init];
    LocationManager.delegate=self;
    LocationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [LocationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSString * latitude = [[[NSString alloc] initWithFormat:@"%f", newLocation.coordinate.latitude]autorelease];
    NSString * longitude = [[[NSString alloc] initWithFormat:@"%f", newLocation.coordinate.longitude]autorelease];

    [LocationManager stopUpdatingLocation];

    NSLog(@"latitude:%@",latitude);
    NSLog(@"longitude:%@",longitude);
}
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
SampathKumar
  • 2,525
  • 8
  • 47
  • 82

6 Answers6

5
    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation                     *)newLocation fromLocation:(CLLocation *)oldLocation  

    {    
    [manager stopUpdatingLocation];    
    lati = [[NSString alloc] initWithFormat:@"%+.6f", newLocation.coordinate.latitude];    
    NSLog(@"address:%@",lati);

    longi = [[NSString alloc] initWithFormat:@"%+.6f", newLocation.coordinate.longitude];
    NSLog(@"address:%@",longi);

    [geoCoder reverseGeocodeLocation: newLocation completionHandler: ^(NSArray *placemarks, NSError *error) 
     {
         //Get nearby address
         CLPlacemark *placemark = [placemarks objectAtIndex:0];

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

         //Print the location to console
         NSLog(@"I am currently at %@",locatedAt);

         address = [[NSString alloc]initWithString:locatedAt];    
         NSLog(@"address:%@",address);
     }];

}
AppleDelegate
  • 4,269
  • 1
  • 20
  • 27
iPhone Programmatically
  • 1,211
  • 2
  • 22
  • 54
5
CLGeocoder *ceo = [[CLGeocoder alloc]init];
        CLLocation *loc = [[CLLocation alloc]initWithLatitude:32.00 longitude:21.322];

     [ceo reverseGeocodeLocation: loc completionHandler:
     ^(NSArray *placemarks, NSError *error) {
          CLPlacemark *placemark = [placemarks objectAtIndex:0];
         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);



      }];
AppleDelegate
  • 4,269
  • 1
  • 20
  • 27
  • I have one doubt, Now i am in Coimbatore city in TamilNadu state, but i recieve address in Apple Store, San Francisco, 1 Stockton St, San Francisco, CA 94108-5805, United States. – SampathKumar Oct 19 '12 at 06:56
  • Run it on device..simulator location points to the above address – AppleDelegate Oct 19 '12 at 06:58
1

You have ta make some reverse location. You're lucky : Apple provides a class to do that. See CLGeocoder (for iOS >= 5.0) or MKReverseGeocoder (for iOS < 5.0)

Pierre
  • 10,593
  • 5
  • 50
  • 80
1

You can use Google Maps API for this (works on any iOS):

NSString *req = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%.5f,%.5f&sensor=false&language=da", location.latitude, location.longitude];
NSString *resultString = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];

Then you can parse this response to NSDictionary using Any JSON library (SBJSON in this case):

SBJSON *jsonObject = [[SBJSON alloc] init];
NSError *error = nil;
NSDictionary *response = [jsonObject objectWithString:resultString error:&error];
[jsonObject release];

Extracting address:

if (error) {
        NSLog(@"Error parsing response string to NSObject: %@",[error localizedDescription]);

    }else{

        NSString *status = [response valueForKey:@"status"];


        if ([status isEqualToString:@"OK"]) {

            NSArray *results = [response objectForKey:@"results"];

            int count = [results count];

            //NSSLog(@"Matches: %i", count);


            if (count > 0) {
                NSDictionary *result = [results objectAtIndex:0];


                NSString *address = [result valueForKey:@"formatted_address"];



            }

        }

    }
Lukasz
  • 19,816
  • 17
  • 83
  • 139
0

You have to create a CLLocation object and pass that on to reverseGeoCoder.

zahreelay
  • 1,742
  • 12
  • 18
0

Before iOS 5.0 we have MKReverse Geo-coder class to find this..Now it is deprecated..

We have to use CLGeocoder class in Core Location Framework

Sandeep
  • 31
  • 1
  • 3