2010-06-24 181 views

回答

6

首先你需要一個實現協議CLLocationManagerDelegate的類。所以,你至少需要實現的方法:

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
... 
} 

之後,創建CLLocationManager的一個實例,設置委託並開始更新位置:

CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate  = self; //SET YOUR DELEGATE HERE 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; //SET THIS TO SPECIFY THE ACCURACY 
[locationManager startUpdatingLocation]; 

調用startUpdatingLocation後,您的locationManager: didUpdateToLocation: fromLocation執行得一旦發生定位錯誤,就立即調用。參數newLocation包含您的實際位置。但是位置管理者會盡快修復一個位置,即使沒有給出您指定的準確性。對於這種情況,您必須自行檢查新位置的準確性。

歡呼聲, 安卡

+0

嗨,在'locationManager'方法,我可以存儲經度/緯度到瓦爾在應用以後使用?像這樣:' - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { float long = newLocation.longitude; float lat = newLocation.latitude; }' – Luca 2011-05-15 20:50:36