2010-11-27 12 views
1

私はiPhoneの現在位置の位置をつかむしようとしていると私は私が間違っているのかわからないです、何もログに記録していないか、表示されます....グラビングGPS場所iPhone 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]; 

順番に:longとlatを取得し、Google Map APIで変換し、アプリが起動するときにはまずlocationManagerを呼び出します。

座標は、単に実際の場所に変換するには

答えて

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

このデリゲートは、いくつかのiOS SDKの上では動作しません発生しません表示されます。デバイスでテストしてチェックしましたか?

+0

デリゲートがうまく動作すれば、シミュレータの座標は完全に動作しますが、アドレスに変換するためには、他のデリゲートが動作するようにデバイスをテストする必要がありますか? – Anthony

+0

私はデバイスでそれをテストしました。単純にこれが起こります。そのデリゲートはうまく動作しますが、その座標を取得しますが、statusText.textの座標を取得してその座標に移動する必要があります – Anthony

0

MapKitにはMKReverseGeocoderというクラスがあります。受信した位置座標で初期化して起動します。デリゲートからMKPlacemarkを返します。住所、市町村、都道府県、郵便番号、国などにアクセスするにはMKPlacemarkを使用してください。

0

米国と他の国々(緯度経度を含む)の無料郵便番号データベースを取得してから、近くでテストしてください。その後、APIに依存することはありません。