iPhone - Get City name from Latitude and Longtiude

前端 未结 10 949
难免孤独
难免孤独 2020-12-05 06:04

I want get my Current City name in my iPhone App.

I\'m currently getting the latitude and longitude using CLLocationManager and than i am passing my coordinates into

10条回答
  •  没有蜡笔的小新
    2020-12-05 06:25

    As per documentation CLGeocoder does not work below iOS5. You need to take another route to support iOS4 and iOS5 both.

    You can look at MKReverseGeocoder, however it is deprecated in iOS5 but still will serve the purpose. For confirmation you can check so called question

    +(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];
        locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        return [locationString substringFromIndex:6];
    }
    

    You can use this function for getting address from latitude, longitude. You can change according to requirement. We put this as Class method so we can directly use it as

    NSString *strAddressFromLatLong = [CLassName getAddressFromLatLon:37.484848 withLongitude:74.48489];
    

    EDIT

    Please stop using above function as it has stopped working reported in comments (Not tested by me). I recommend to start using SVGeocoder

提交回复
热议问题