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

首頁 > 系統 > iOS > 正文

IOS開發中地理位置定位系統的操作方法

2020-02-19 15:53:30
字體:
來源:轉載
供稿:網友

IOS開發中地理位置定位系統的操作方法大家了解嗎?其實這個功能在IOS系統中是很常見的,今天武林技術頻道小編就為大家簡單介紹一下吧,希望對你了解這方面知識有幫助!

IOS開發中地理位置定位系統的操作方法

這是因為xcode升級造成的定位權限設置問題。
升級xcode6、xcode7以后打開以前xcode5工程,程序不能定位。工程升級到xcode6或xcode7編譯時需要iOS8 要自己寫授權,不然沒權限定位。

解決方法:

首先在 info.plist里加入對應的缺省字段 ,值設置為YES(前臺定位寫上邊字段,前后臺定位寫下邊字段)
NSLocationWhenInUseUsageDescription //允許在前臺獲取GPS的描述
NSLocationAlwaysUsageDescription //允許在前、后臺獲取GPS的描述

設置的圖示:

?

好了,如果設置好了,那就正式進入編碼學習吧,首先熟悉蘋果提供的關于定位服務相關的類,方法以及屬性:

1、定位服務和地圖應用的介紹

定位服務: 獲取用戶當前的位置信息,針對用戶的位置信息做相關的數據處理。

地圖應用: 根據實際需求展示地圖和周邊環境信息,基于用戶當前位置展示用戶所關注的地圖位置信息、以及為用戶導航。

?定位服務要掌握的:

?主要操作的類:CLLocationManager

?所屬庫:CoreLocation

?結構體:CLLocationCoordinate2D(經緯度)、CLCLocationCoorRegion(區域)

?地圖應用需要掌握的:

?框架:MapKit

?操作類:MKMapView

2、定位服務

?屬性:

?desiredAccuracy設置定位精確度,這是一個常量屬性,一般用best
?distanceFilter 重新定位的最小變化距離

方法:

?設置什么時候開啟定位的狀態 ?requestAlwaysAuthorization() 始終開啟定位
?requestWhenInUseAuthorization() 當app進入前臺的時候開啟定位(iOS8的新方法)
?類方法locationServicesEnabled() 是否有定位服務功能(CLLocationManager)
?startUpdatingLocation() 開啟定位

代理:

?代理的協議:
?代理的方法:可以直接進入這個庫的API查看,只要就是定位錯誤調用的代理方法,定位成功調用的代理方法等等;

涉及到的對象

?locations: CLLocation 該CLLocation對象的屬性: ?coordinate ?longitude/latitude

英語詞匯積累:

?accuracy 英 ///'?kj?r?s? n. [數] 精確度,準確性
?filter 英 ///'f?lt? 濾波器 過濾器;篩選;濾光器 過濾;滲透;用過濾法除去

下面提供的是Swift源碼:

//// ViewController.swift// LocationManager//// Created by HEYANG on //.// Copyright ? 年 HEYANG. All rights reserved.//import UIKit// 需要導入CoreLocation框架import CoreLocationclass ViewController: UIViewController,CLLocationManagerDelegate {// 聲明一個全局變量var locationManager:CLLocationManager!override func viewDidLoad() {super.viewDidLoad()locationManager = CLLocationManager()// 設置定位的精確度locationManager.desiredAccuracy = kCLLocationAccuracyBest// 設置定位變化的最小距離 距離過濾器locationManager.distanceFilter = // 設置請求定位的狀態if #available(iOS ., *) {locationManager.requestWhenInUseAuthorization()} else {// Fallback on earlier versionsprint(///"hello///")}//這個是在ios之后才有的// 設置代理為當前對象locationManager.delegate = self;if CLLocationManager.locationServicesEnabled(){// 開啟定位服務locationManager.startUpdatingLocation()}else{print(///"沒有定位服務///")}}// 定位失敗調用的代理方法func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {print(error)}// 定位更新地理信息調用的代理方法func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {if locations.count > {let locationInfo = locations.last!let alert:UIAlertView = UIAlertView(title: ///"獲取的地理坐標///",message: ///"經度是:////(locationInfo.coordinate.longitude),維度是:////(locationInfo.coordinate.latitude)///",delegate: nil, cancelButtonTitle: ///"是的///")alert.show()}}}

下面是Objective-C的源碼:

//// ViewController.m// LocationManager//// Created by HEYANG on //.// Copyright ? 年 HEYANG. All rights reserved.//#import ///"ViewController.h///"#import @interface ViewController () /** 全局定位對象 */@property (nonatomic,strong)CLLocationManager *locationManager;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];CLLocationManager* locationManager = [[CLLocationManager alloc] init];// 設置定位精確度locationManager.desiredAccuracy = kCLLocationAccuracyBest;// 設置定位變化最小距離locationManager.distanceFilter = ;// 設置定位服務的使用狀態[locationManager requestWhenInUseAuthorization]; locationManager.delegate = self;if ([CLLocationManager locationServicesEnabled]) {[locationManager startUpdatingLocation];}else{NSLog(@///"本機不支持定位服務功能///");}self.locationManager = locationManager;}// 定位失敗調用的代理方法-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{NSLog(@///"錯誤信息:%@///",error);}// 定位數據更新調用的代理方法-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{if (locations.count > ) {CLLocation* location = locations.lastObject;CLLocationCoordinateD coordinateD = location.coordinate;NSString* message = [NSString stringWithFormat:@///"經度:%lf,維度是:%lf///",coordinateD.longitude,coordinateD.latitude];UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@///"顯示當前位置的經緯度///"                 message:message delegate:nil cancelButtonTitle:@///"取消///" otherButtonTitles:@///"確定///", nil];[alertView show];}}@end 

以上是武林技術頻道小編給大家分享的IOS開發中地理位置定位系統的操作方法,希望對大家有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涡阳县| 灵川县| 镇原县| 扶绥县| 屯门区| 洛浦县| 杭州市| 平潭县| 兰西县| 根河市| 永福县| 绩溪县| 乌兰察布市| 阳江市| 如皋市| 南溪县| 白山市| 卓尼县| 平顺县| 文水县| 多伦县| 太保市| 三都| 鄱阳县| 万荣县| 读书| 昭通市| 县级市| 天门市| 台州市| 陈巴尔虎旗| 安平县| 江油市| 汽车| 辽中县| 兴城市| 黔南| 郸城县| 东乡县| 新巴尔虎右旗| 沛县|