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

首頁(yè) > 系統(tǒng) > iOS > 正文

IOS入門(mén)筆記之地理位置定位系統(tǒng)

2020-07-26 03:29:02
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

本章將提供Swift版本和Objective-C兩個(gè)版本的入門(mén)代碼,分別實(shí)現(xiàn)顯示當(dāng)前手機(jī)或者是模擬器的地理經(jīng)緯度坐標(biāo)。

寫(xiě)在正式學(xué)習(xí)前的小貼士:

這是因?yàn)閤code升級(jí)造成的定位權(quán)限設(shè)置問(wèn)題。
升級(jí)xcode6、xcode7以后打開(kāi)以前xcode5工程,程序不能定位。工程升級(jí)到xcode6或xcode7編譯時(shí)需要iOS8 要自己寫(xiě)授權(quán),不然沒(méi)權(quán)限定位。

解決方法:

首先在 info.plist里加入對(duì)應(yīng)的缺省字段 ,值設(shè)置為YES(前臺(tái)定位寫(xiě)上邊字段,前后臺(tái)定位寫(xiě)下邊字段)
NSLocationWhenInUseUsageDescription //允許在前臺(tái)獲取GPS的描述
NSLocationAlwaysUsageDescription //允許在前、后臺(tái)獲取GPS的描述

設(shè)置的圖示:


好了,如果設(shè)置好了,那就正式進(jìn)入編碼學(xué)習(xí)吧,首先熟悉蘋(píng)果提供的關(guān)于定位服務(wù)相關(guān)的類(lèi),方法以及屬性:

1、定位服務(wù)和地圖應(yīng)用的介紹

定位服務(wù): 獲取用戶當(dāng)前的位置信息,針對(duì)用戶的位置信息做相關(guān)的數(shù)據(jù)處理。

地圖應(yīng)用: 根據(jù)實(shí)際需求展示地圖和周邊環(huán)境信息,基于用戶當(dāng)前位置展示用戶所關(guān)注的地圖位置信息、以及為用戶導(dǎo)航。

•定位服務(wù)要掌握的:

•主要操作的類(lèi):CLLocationManager

•所屬庫(kù):CoreLocation

•結(jié)構(gòu)體:CLLocationCoordinate2D(經(jīng)緯度)、CLCLocationCoorRegion(區(qū)域)

•地圖應(yīng)用需要掌握的:

•框架:MapKit

•操作類(lèi):MKMapView

2、定位服務(wù)

•屬性:

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

方法:

•設(shè)置什么時(shí)候開(kāi)啟定位的狀態(tài) •requestAlwaysAuthorization() 始終開(kāi)啟定位
•requestWhenInUseAuthorization() 當(dāng)app進(jìn)入前臺(tái)的時(shí)候開(kāi)啟定位(iOS8的新方法)
•類(lèi)方法locationServicesEnabled() 是否有定位服務(wù)功能(CLLocationManager)
•startUpdatingLocation() 開(kāi)啟定位

代理:

•代理的協(xié)議:
•代理的方法:可以直接進(jìn)入這個(gè)庫(kù)的API查看,只要就是定位錯(cuò)誤調(diào)用的代理方法,定位成功調(diào)用的代理方法等等;

涉及到的對(duì)象

•locations: CLLocation 該CLLocation對(duì)象的屬性: •coordinate •longitude/latitude

英語(yǔ)詞匯積累:

•accuracy 英 'ækjʊrəsɪ n. [數(shù)] 精確度,準(zhǔn)確性
•filter 英 'fɪltə 濾波器 過(guò)濾器;篩選;濾光器 過(guò)濾;滲透;用過(guò)濾法除去

下面提供的是Swift源碼:

//// ViewController.swift// LocationManager//// Created by HEYANG on //.// Copyright © 年 HEYANG. All rights reserved.//import UIKit// 需要導(dǎo)入CoreLocation框架import CoreLocationclass ViewController: UIViewController,CLLocationManagerDelegate {// 聲明一個(gè)全局變量var locationManager:CLLocationManager!override func viewDidLoad() {super.viewDidLoad()locationManager = CLLocationManager()// 設(shè)置定位的精確度locationManager.desiredAccuracy = kCLLocationAccuracyBest// 設(shè)置定位變化的最小距離 距離過(guò)濾器locationManager.distanceFilter = // 設(shè)置請(qǐng)求定位的狀態(tài)if #available(iOS ., *) {locationManager.requestWhenInUseAuthorization()} else {// Fallback on earlier versionsprint("hello")}//這個(gè)是在ios之后才有的// 設(shè)置代理為當(dāng)前對(duì)象locationManager.delegate = self;if CLLocationManager.locationServicesEnabled(){// 開(kāi)啟定位服務(wù)locationManager.startUpdatingLocation()}else{print("沒(méi)有定位服務(wù)")}}// 定位失敗調(diào)用的代理方法func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {print(error)}// 定位更新地理信息調(diào)用的代理方法func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {if locations.count > {let locationInfo = locations.last!let alert:UIAlertView = UIAlertView(title: "獲取的地理坐標(biāo)",message: "經(jīng)度是:/(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>/** 全局定位對(duì)象 */@property (nonatomic,strong)CLLocationManager *locationManager;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];CLLocationManager* locationManager = [[CLLocationManager alloc] init];// 設(shè)置定位精確度locationManager.desiredAccuracy = kCLLocationAccuracyBest;// 設(shè)置定位變化最小距離locationManager.distanceFilter = ;// 設(shè)置定位服務(wù)的使用狀態(tài)[locationManager requestWhenInUseAuthorization]; locationManager.delegate = self;if ([CLLocationManager locationServicesEnabled]) {[locationManager startUpdatingLocation];}else{NSLog(@"本機(jī)不支持定位服務(wù)功能");}self.locationManager = locationManager;}// 定位失敗調(diào)用的代理方法-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{NSLog(@"錯(cuò)誤信息:%@",error);}// 定位數(shù)據(jù)更新調(diào)用的代理方法-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{if (locations.count > ) {CLLocation* location = locations.lastObject;CLLocationCoordinateD coordinateD = location.coordinate;NSString* message = [NSString stringWithFormat:@"經(jīng)度:%lf,維度是:%lf",coordinateD.longitude,coordinateD.latitude];UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"顯示當(dāng)前位置的經(jīng)緯度"                 message:message delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];[alertView show];}}@end 

以上是小編給大家分享的IOS入門(mén)筆記之地理位置定位系統(tǒng),希望對(duì)大家有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 驻马店市| 阳泉市| 华容县| 石景山区| 大理市| 余干县| 潍坊市| 潢川县| 中方县| 绥芬河市| 临漳县| 门头沟区| 伊川县| 轮台县| 崇阳县| 大宁县| 濉溪县| 瓮安县| 新竹市| 山阳县| 城市| 连云港市| 永安市| 玉环县| 安溪县| 城固县| 通山县| 望奎县| 射洪县| 顺昌县| 祥云县| 印江| 昂仁县| 菏泽市| 仁布县| 修文县| 华池县| 滦南县| 浑源县| 金塔县| 龙海市|