[iPhone程式]如何取得使用者目前的座標位置

来源:互联网 发布:tiva数据库 编辑:程序博客网 时间:2024/05/17 22:31
☉目標:在iPhone應用程式中取得使用者目前的經緯度。 

☉效果畫面: 
這裡借用了http://iphone4.tw的圖片。自己懶得Print Screen...XD 
03 
我們可以透過CLLocationManager類別取得使用者座標位置。
CLLocationManager *locationManager = [[CLLocationManager alloc] init];locationManager.delegate=self;locationManager.desiredAccuracy=kCLLocationAccuracyBest;[locationManager startUpdatingLocation];

☉步驟說明:

Step(1):首先,先建立一個CLLocationManager類別的物件。 


Step(2):接著指定委派的對象,這裡是指定自己(self)。 





Step(3):最後呼叫CLLocationManager類別的一個方法「startUpdatingLocation」,這個時候如果使用者是第一次使用你的iPhone軟體,iPhone就會詢問使用者是否允許iPhone軟體取得使用者座標。 





Step(4):當你點選確定後,iPhone就會開始定位,定位完成後就會觸發「didUpdateToLocation」事件。 











因此,當定位完成你可以用以下方法取得使用者座標後,接著進行一些處理。 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{NSString *userLat = [[NSNumber numberWithDouble:newLocation.coordinate.latitude] stringValue];NSString *userLng = [[NSNumber numberWithDouble:newLocation.coordinate.longitude] stringValue];}
原创粉丝点击