2012-10-19 54 views
3

目前我正在iPhone应用程序中工作,使用CLLocationManager获取经纬度值的罚款。 我不知道这个?如何从此经度和纬度值获取设备位置(地址)名称?请帮我如何在iPhone中获取设备位置名称?

由于提前

我尝试这样做:

- (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); 
} 
+1

这应做到:HTTP ://stackoverflow.com/a/9875424/624091 – George

回答

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); 
    }]; 

} 
1

您可以使用谷歌地图API本(适用于任何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]; 

然后你就可以(在这种情况下SBJSON)解析使用任何JSON库的NSDictionary这样的响应:

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

提取地址:

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"]; 



      } 

     } 

    } 
+0

感谢您的回复,现在我检查它尽快更新, – SampathKumar

+0

我编辑/缩短了问题并删除了过时的代码。 – Lukasz

0

BEF矿石的iOS 5.0,我们有MKReverse地理编码器类,以找到this..Now它已被弃用..

我们必须使用CLGeocoder类Core Location框架

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); 



     }]; 
+0

感谢您的回复 – SampathKumar

+0

我有一个疑问,现在我在哥印拜陀市泰米尔纳德邦的状态,但我在苹果商店,旧金山,1斯托克顿街,旧金山,CA 94108-5805,美国收到的地址。 – SampathKumar

+0

在设备上运行它..模拟器位置指向上述地址 – AppleDelegate