2009-11-23 6 views
0

GPS에있는 사람을 찾는 프로그램을 추가하려하지만 사람이있는 상태를 값으로 설정합니다. 예 : GPS 자신의 아이폰에서 사람을 찾은 다음 해당 상태를 반환합니다. 캘리포니아 주 변수는 캘리포니아에 문자열로 설정됩니다. 누군가 도움을 주시면 감사하겠습니다!아이폰 GPS에서 주 위치를 찾는 방법?

답변

2

Core Location을 사용하여 위치를 찾은 다음 MKReverseGeocoder을 사용하여 위치에서 상태를 가져올 수 있습니다.

+3

그냥 경고 - 다른 사람이 여기에 게시 한로 (HTTP ://stackoverflow.com/questions/918423/using-the-google-maps-api-for-reverse-geocoding-lat-long-from-iphone/921165#921165), Google Maps API의 서비스 약관에 위배됩니다. 해당 Google지도를 표시하지 않는 한 데이터를 지오 코딩합니다. (서비스 약관의 섹션 10.12 참조 : http://code.google.com/apis/maps/iphone/terms.html) – delfuego

+0

이러한 방법을 함께 사용하는 방법에 대한 샘플이 있습니까? – Silent

0

당신이해야 할 일은 현재 좌표를 찾을 CLLocationManager를 설정하는 것입니다. 현재 좌표로 MKReverseGeoCoder를 사용하여 위치를 찾아야합니다. 위의 코드에 대한

 
- (void)viewDidLoad 
{ 
    // this creates the CCLocationManager that will find your current location 
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; 
    [locationManager startUpdatingLocation]; 
} 

// this delegate is called when the app successfully finds your current location 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    // this creates a MKReverseGeocoder to find a placemark using the found coordinates 
    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate]; 
    geoCoder.delegate = self; 
    [geoCoder start]; 
} 

// this delegate method is called if an error occurs in locating your current location 
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
NSLog(@"locationManager:%@ didFailWithError:%@", manager, error); 
} 
// this delegate is called when the reverseGeocoder finds a placemark 
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark 
{ 
    MKPlacemark * myPlacemark = placemark; 
    // with the placemark you can now retrieve the city name 
    NSString *city = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressStateKey]; 
} 

// this delegate is called when the reversegeocoder fails to find a placemark 
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error 
{ 
    NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error); 
} 
0

불과 몇 수정 : 당신이 kABPersonAddressStateKey의 컴파일에 문제가있는 경우 kABPersonAddressStateKey 당신에게 국가가 아닌 도시
2를 줄 것이다 사용
1. 프로젝트에 주소록 프레임 워크를 추가 과 같습니다 : 당신의하는 .m 파일

0

에서

#import <AddressBook/AddressBook.h> 

가 역 지오 코딩을 사용하여 당신이 필요하고 간단합니다.

CLLocation * 위치 속성을 보유하고있는 클래스가 있다면 내 프로젝트 중 하나에 대해 작성한이 코드 스 니퍼를 사용하면됩니다. 당신이 D에 점점 더 많은 관심을 가질 것입니다

-(void)loadAddress:(void (^)(NSError *error))completion { 
    CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 

    [geocoder reverseGeocodeLocation:self.location 
        completionHandler:^(NSArray *placemarks, NSError *error) { 

         NSString *address = nil; 

         if (error) { 
          self.placemark = nil; 
          self.address = NSLocalizedString(@"Unknown address.",@""); 
         } 
         else {       
          CLPlacemark * placeMark = [placemarks firstObject]; 

          self.placemark = placeMark; 

          NSDictionary *d = placeMark.addressDictionary; 
          address = [(NSArray*)d[@"FormattedAddressLines"] componentsJoinedByString:@" "]; 
          self.address = address; 
         } 

         // Call caller 
         if (completion) completion(error); 
        }]; 

} 

주 [을 @ "주"] 대신 D보다는 [@ "FormattedAddressLines"]. 역 지오 코딩에는 인터넷 액세스가 필요하며 (웹 서비스로 구현 됨) 볼륨 제한이 있습니다. 대부분 분당 두 개 이상의 통화를 초과해서는 안됩니다. Apple에서 설정 한 용량을 초과하면 오류가 발생합니다. 여러분의 편의를 위해

, 여기에 placeMark.addressDictionary proprerty에 의해 저장된 KV 있습니다 MKReverseGeocoder가 역 지오 코딩을 할 구글을 사용

{ 
    City = Millbrae; 
    Country = "Etats-Unis"; 
    CountryCode = US; 
    FormattedAddressLines =  (
     "I-280 N", 
     "Half Moon Bay, CA 94019", 
     "Etats-Unis" 
    ); 
    Name = "I-280 N"; 
    State = CA; 
    Street = "I-280 N"; 
    SubAdministrativeArea = "San Mat\U00e9o"; 
    SubLocality = "Bay Area"; 
    Thoroughfare = "I-280 N"; 
    ZIP = 94019; 
} 
관련 문제