2010-11-27 80 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]; 

爲了抓住漫長的和經過的時間,在谷歌地圖API中轉換它,並在應用啓動時首先調用locationManager。

座標出現只是簡單的把它們轉換成實際位置不發生

回答

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

這代表將無法在某些iOS版SDK的工作。你有沒有在設備上測試並檢查。

+0

委託工作正常,它抓住了模擬器完美工作的座標,但爲了將它轉換爲地址,我是否還需要在我的設備上測試其他代表的工作? – Anthony 2010-11-27 16:17:22

0

MapKit中有一個名爲MKReverseGeocoder的類。您使用您收到的位置的座標初始化它並啓動它。它將從其代表返回一個MKPlacemark。使用MKPlacemark訪問地址,城市,州,郵政編碼,國家等東西。

0

您還可以獲取美國和其他一些免費的郵政編碼數據庫(包括緯度/經度),然後測試最近的。那麼就不會依賴API。