国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS獲取到用戶當前位置

2019-10-21 18:52:45
字體:
來源:轉載
供稿:網友

通過CoreLocation定位,獲取到用戶當前位置,跟地圖中的定位不同。

一、導入CoreLocation.framework

二、#import <CoreLocation/CoreLocation.h>

三、聲明代理 <CLLocationManagerDelegate>

四、代碼實現

1、聲明

CLLocationManager *locationManager;//定義Manager// 判斷定位操作是否被允許if([CLLocationManager locationServicesEnabled]) {  CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];   self.locationManager.delegate = self;}else {   //提示用戶無法進行定位操作}// 開始定位[locationManager startUpdatingLocation];

2、更新位置后代理方法,iOS6.0一下的方法

- (void)locationManager:(CLLocationManager *)manager   didUpdateToLocation:(CLLocation *)newLocation       fromLocation:(CLLocation *)oldLocation { //latitude和lontitude均為NSString型變量    //緯度 self.latitude = [NSString stringWithFormat:@"%.4f", newLocation.coordinate.latitude];    //經度 self.longitude = [NSString stringWithFormat:@"%.4f",     newLocation.coordinate.longitude]; }

3、iOS6.0以上蘋果的推薦方法

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{  //此處locations存儲了持續更新的位置坐標值,取最后一個值為最新位置,如果不想讓其持續更新位置,則在此方法中獲取到一個值之后讓locationManager stopUpdatingLocation  CLLocation *currentLocation = [locations lastObject];    CLLocationCoordinate2D coor = currentLocation.coordinate;  self.latitude = coor.latitude;  self.longitude = coor.longitude;    //[self.locationManager stopUpdatingLocation];  }

4、更新失敗的方法

- (void)locationManager:(CLLocationManager *)manager    didFailWithError:(NSError *)error {  if (error.code == kCLErrorDenied) {   // 提示用戶出錯原因,可按住Option鍵點擊 KCLErrorDenied的查看更多出錯信息,可打印error.code值查找原因所在 }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东乌珠穆沁旗| 若羌县| 屯留县| 湖北省| 陈巴尔虎旗| 新河县| 定西市| 阜新市| 靖西县| 教育| 绿春县| 台湾省| 文安县| 通辽市| 南城县| 巧家县| 绵竹市| 万载县| 麦盖提县| 绥德县| 蕲春县| 酒泉市| 永嘉县| 绥芬河市| 军事| 岳普湖县| 仪征市| 临城县| 大化| 祁阳县| 佳木斯市| 长岭县| 文山县| 徐州市| 佛坪县| 体育| 彰化市| 安多县| 尉氏县| 冀州市| 万载县|