2010-05-03 40 views
1

是否有方法將用戶位置作爲字符串從模型中返回?將用戶經緯度返回爲字符串iPhone

我有一個模型,它的工作是從Web服務下載相同的JSON數據。在發送請求時,我需要在字符串的末尾添加?lat = LAT_HERE & lng = LNG_HERE。

我見過很多使用地圖或不斷更新標籤的例子。但我不能找出如何顯式返回lat和lng值。

只有2個IM天爲iPhone開發這樣下去容易對我:)

回答

2

你需要利用的核心位置,特別是CLLocationManager。 Apple不提供任何CL編程指南,因此只需查看LocateMe之類的示例即可瞭解如何執行此操作。

1

您需要使用CLLocationManager這樣的:

- (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 
    { 
     // retrieve lat and lng in a string from newLocation.coordinate 
    NSString *lat = [NSString stringWithFormat:@"%d", newLocation.coordinate.latitude]; 
    NSString *lng = [NSString stringWithFormat:@"%d", newLocation.coordinate.longitude]; 
    } 

    // 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); 
    }