1

Possible Duplicate:
How to retrieve user's current city name?

using the following methods i am getting latitude and longitude of the current position

- (void)viewDidLoad {
  [super viewDidLoad];
  locationManager = [[CLLocationManager alloc] init];
  locationManager.delegate = self;
  locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
  locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
  [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
  int degrees = newLocation.coordinate.latitude;
  double decimal = fabs(newLocation.coordinate.latitude - degrees);
  int minutes = decimal * 60;
  double seconds = decimal * 3600 - minutes * 60;
  NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                   degrees, minutes, seconds];
  latLabel.text = lat;
  degrees = newLocation.coordinate.longitude;
  decimal = fabs(newLocation.coordinate.longitude - degrees);
  minutes = decimal * 60;
  seconds = decimal * 3600 - minutes * 60;
  NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                     degrees, minutes, seconds];
  longLabel.text = longt;
}

now i need to get the current location name as a string... but not required on a map, how can i do this, give me some suggestion pls,,,thanx in advance

Community
  • 1
  • 1
Ravi
  • 1,759
  • 5
  • 20
  • 38

5 Answers5

3

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

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
3
- (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);
        }    
    }];
}
sElanthiraiyan
  • 6,000
  • 1
  • 31
  • 38
  • i can get current loction but i can't get current address and i also get null value. plz help me and guide me – Raju Nov 09 '13 at 10:45
  • @AppleMap: there are multiple reasons for this. i) GeoCoder itself doesn't has address of you location. ii)You have to wait for block to complete.then inside placemark dictionary has multiple values , you can check those key values for you need. – EXC_BAD_ACCESS Dec 04 '13 at 05:49
3

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.

Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
  • i am passing this url and lati.&longi. http://maps.google.com/maps/geo?q=23.030000,72.580000&output=csv and i am getting 610,0,0,0 so what is this ? i want to get current city name so plz help me – Raju Nov 09 '13 at 10:05
  • getting 610,0,0,0 .. API stopped working. Please update your answer. – Akshit Zaveri Dec 03 '13 at 11:18
2

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

EXC_BAD_ACCESS
  • 2,699
  • 2
  • 21
  • 25
0

Maybe this is what you will have to use MKReverseGeocoder

Shantanu
  • 3,086
  • 3
  • 25
  • 32
  • MKReverseGeocoder is deprecated in like iOS 4.0 and later. So he should use CLGeocoder. –  Jul 02 '12 at 19:04