2010-11-27 8 views
1

내가 아이폰의 현재 위치의 위치를 ​​잡기 위해 노력하고 그리고 난 내가 잘못, 아무것도 로그를 수행하거나 표시하고 무엇을 잘 모르겠습니다 ....잡는 GPS 위치 아이폰 SDK

코드가 여기에 있습니다 :

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    int degrees = newLocation.coordinate.latitude; 
    double decimal = fabs(newLocation.coordinate.latitude - degrees); 
    int minutes = decimal * 60; 
    double seconds = decimal * 3600 - minutes * 60; 
    NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
      degrees, minutes, seconds]; 
    statusText.text = lat; 
    degrees = newLocation.coordinate.longitude; 
    decimal = fabs(newLocation.coordinate.longitude - degrees); 
    minutes = decimal * 60; 
    seconds = decimal * 3600 - minutes * 60; 
    NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
      degrees, minutes, seconds]; 
    statusText.text = longt; 


} 

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address 
{ 
    double latitude = 0, longitude = 0; 
    NSString *esc_addr = [statusText.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr]; 
    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL]; 
    if (result) { 
     NSScanner *scanner = [NSScanner scannerWithString:result]; 
     if ([scanner scanUpToString:@"\"lat\":" intoString:nil] && [scanner scanString:@"\"lat\":" intoString:nil]) { 
      [scanner scanDouble:&latitude]; 
      if ([scanner scanUpToString:@"\"lng\":" intoString:nil] && [scanner scanString:@"\"lng\":" intoString:nil]) { 
       [scanner scanDouble:&longitude]; 
      } 
     } 
    } 
    CLLocationCoordinate2D center; 
    center.latitude = latitude; 
    center.longitude = longitude; 
    return center; 
} 



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m 
    [locationManager startUpdatingLocation]; 

순서대로 : 위도와 경도를 잡고 Google지도 API로 변환 한 다음 앱이 시작될 때 locationManager를 먼저 호출합니다.

좌표는 단지 ​​실제 위치로 변환이

답변

0
- (void)locationManager:(CLLocationManager *)manager 
     didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 

이 대표는 일부 아이폰 OS SDK의에서 작동하지 않습니다 발생하지 않습니다 나타납니다. 장치에서 테스트하고 확인 했습니까?

+0

대리자가 잘 작동하지만 시뮬레이터와 완벽하게 작동하는 좌표를 가져 오지만 주소로 변환하려면 다른 위임자가 작동하도록 장치에서 테스트해야합니까? – Anthony

+0

장치에서 테스트했습니다. 간단히 이것은 일어나는 일입니다. 그 위임자가 잘 작동하지만 좌표를 잡으면 위치 좌표로 가져온 statusText.text 좌표를 원한다. – Anthony

0

MapKit에는 MKReverseGeocoder라는 클래스가 있습니다. 받은 위치의 좌표로 초기화하고 시작합니다. 대리인의 MKPlacemark를 반환합니다. 주소, 도시, 주, 우편 번호, 국가 등을 액세스하려면 MKPlacemark를 사용하십시오.

0

미국 및 일부 다른 국가 (위도/경도 포함)에서 무료 우편 번호 데이터베이스를 가져 와서 가장 가까운 곳을 테스트 할 수도 있습니다. 그렇다면 API에 의존 할 필요가 없습니다.