2011-06-21 61 views
0

我在iOS中使用核心位置來獲取GPS座標。我想獲得這些座標所在的一般區域的一些人類可讀的文本描述。我可以在設備上本地或在我提交座標的基於PHP的網站上執行此操作。從GPS座標獲取地區的文本說明

任何想法?

回答

0

我在我的應用程序中使用CLLocationManagerDelegate和MKReverseGeocoderDelegate。創建CLLocationManager * locationManager然後開始設置它的屬性和準確性。

- (void)locationManager:(CLLocationManager *)manager 
     didUpdateHeading:(CLHeading *)newHeading 
{ 
    [manager stopUpdatingHeading]; 

    CLLocationCoordinate2D coordinate = manager.location.coordinate; 

    MKReverseGeocoder * geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate]; 
    geocoder.delegate = self; 
    [geocoder start]; 
} 

#pragma mark - MKReverseGeocoderDelegate 

你會得到NSDictionary的位置信息。我沒有使用所有的鍵。如果您需要超過列出的NSLOG字典鍵及其響應值。 希望它能幫助你。

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark 
{ 
    getLocationState = glsAvailable; 

    NSDictionary * dic = placemark.addressDictionary; 

    NSString * CountryCode = [dic objectForKey:@"CountryCode"]; 
    NSString * State = [dic objectForKey:@"State"]; 
    NSString * City = [dic objectForKey:@"City"]; 
    NSString * SubLocality = [dic objectForKey:@"SubLocality"]; 
    NSString * Street = [dic objectForKey:@"Street"]; 
    NSString * ZIP = [dic objectForKey:@"ZIP"]; 

    self.locationString = [NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@", 
          CountryCode?CountryCode:@"", 
          State?State:@"", 
          City?City:@"", 
          SubLocality?SubLocality:@"", 
          Street?Street:@"", 
          ZIP?ZIP:@"" 
          ]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:@"LOCATION_STRING_IS_READY" object:nil]; 
    [geocoder cancel]; 
    [geocoder release]; 
}