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

首頁 > 系統 > iOS > 正文

IOS入門筆記之地理位置定位系統

2019-10-21 18:56:12
字體:
來源:轉載
供稿:網友
關于地理位置及定位系統,在iOS開發中也比較常見,接下來通過本文給大家介紹IOS入門筆記之地理位置定位系統,對ios地理位置定位系統感興趣的朋友一起學習吧
 

前言:關于地理位置及定位系統,在iOS開發中也比較常見,比如美團外面的餐飲店鋪的搜索,它首先需要用戶當前手機的位置,然后在這個位置附近搜索相關的餐飲店鋪的位置,并提供相關的餐飲信息,再比如最常見的就是地圖導航,地圖導航更需要定位服務,然后根據用戶的目的地選出一條路線。其實,作為手機用戶這么長時間,或多或少會發現在有些app應用首次在你的手機安裝成功后,首次啟動可能就會提示"是否同意XXx(比如百度瀏覽器)獲取當前位置"等這樣一類的信息。可見地理位置及定位系統是企業app開發必不可少的技能。

本章將提供Swift版本和Objective-C兩個版本的入門代碼,分別實現顯示當前手機或者是模擬器的地理經緯度坐標。

寫在正式學習前的小貼士:

這是因為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 <CoreLocation/CoreLocation.h>@interface ViewController () <CLLocationManagerDelegate>/** 全局定位對象 */@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<CLLocation *> *)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入門筆記之地理位置定位系統,希望對大家有所幫助。



注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 孟津县| 贞丰县| 通州区| 将乐县| 柳江县| 漠河县| 渝北区| 兴海县| 德兴市| 资溪县| 甘谷县| 瑞安市| 山东省| 宜章县| 平舆县| 通山县| 龙州县| 恭城| 康马县| 丽江市| 广州市| 屯昌县| 苍山县| 辽阳市| 广东省| 鄯善县| 和龙市| 吕梁市| 建水县| 寻乌县| 拉萨市| 离岛区| 偏关县| 靖边县| 鹤庆县| 巴林左旗| 蓝田县| 融水| 贵德县| 乾安县| 霍林郭勒市|