2012-10-19 57 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