前言:關於地理位置及定位系統,在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
- coordinate
- locations: CLLocation 該CLLocation對象的屬性:
-
英語詞匯積累:
- accuracy 英 'ækjʊrəsɪ n. [數] 精確度,准確性
- filter 英 'fɪltə 濾波器 過濾器;篩選;濾光器 過濾;滲透;用過濾法除去
下面提供的是Swift源碼:
1 // 2 // ViewController.swift 3 // LocationManager 4 // 5 // Created by HEYANG on 16/1/26. 6 // Copyright © 2016年 HEYANG. All rights reserved. 7 // 8 9 import UIKit 10 11 // 需要導入CoreLocation框架 12 import CoreLocation 13 14 class ViewController: UIViewController,CLLocationManagerDelegate { 15 16 // 聲明一個全局變量 17 var locationManager:CLLocationManager! 18 19 override func viewDidLoad() { 20 super.viewDidLoad() 21 locationManager = CLLocationManager() 22 23 // 設置定位的精確度 24 locationManager.desiredAccuracy = kCLLocationAccuracyBest 25 26 // 設置定位變化的最小距離 距離過濾器 27 locationManager.distanceFilter = 50 28 29 // 設置請求定位的狀態 30 if #available(iOS 8.0, *) { 31 locationManager.requestWhenInUseAuthorization() 32 } else { 33 // Fallback on earlier versions 34 print("hello") 35 }//這個是在ios8之后才有的 36 37 // 設置代理為當前對象 38 locationManager.delegate = self; 39 40 if CLLocationManager.locationServicesEnabled(){ 41 // 開啟定位服務 42 locationManager.startUpdatingLocation() 43 }else{ 44 print("沒有定位服務") 45 } 46 47 } 48 // 定位失敗調用的代理方法 49 func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 50 print(error) 51 } 52 // 定位更新地理信息調用的代理方法 53 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 54 if locations.count > 0 55 { 56 let locationInfo = locations.last! 57 let alert:UIAlertView = UIAlertView(title: "獲取的地理坐標", 58 message: "經度是:\(locationInfo.coordinate.longitude),維度是:\(locationInfo.coordinate.latitude)", 59 delegate: nil, cancelButtonTitle: "是的") 60 alert.show() 61 } 62 } 63 }
下面是Objective-C的源碼:
1 // 2 // ViewController.m 3 // LocationManager 4 // 5 // Created by HEYANG on 16/1/26. 6 // Copyright © 2016年 HEYANG. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 #import <CoreLocation/CoreLocation.h> 12 13 14 @interface ViewController () <CLLocationManagerDelegate> 15 16 /** 全局定位對象 */ 17 @property (nonatomic,strong)CLLocationManager *locationManager; 18 19 @end 20 21 @implementation ViewController 22 23 - (void)viewDidLoad { 24 [super viewDidLoad]; 25 26 CLLocationManager* locationManager = [[CLLocationManager alloc] init]; 27 28 // 設置定位精確度 29 locationManager.desiredAccuracy = kCLLocationAccuracyBest; 30 // 設置定位變化最小距離 31 locationManager.distanceFilter = 50; 32 33 // 設置定位服務的使用狀態 34 [locationManager requestWhenInUseAuthorization]; 35 locationManager.delegate = self; 36 37 if ([CLLocationManager locationServicesEnabled]) { 38 [locationManager startUpdatingLocation]; 39 }else{ 40 NSLog(@"本機不支持定位服務功能"); 41 } 42 43 self.locationManager = locationManager; 44 } 45 // 定位失敗調用的代理方法 46 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ 47 NSLog(@"錯誤信息:%@",error); 48 } 49 // 定位數據更新調用的代理方法 50 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{ 51 if (locations.count > 0) { 52 CLLocation* location = locations.lastObject; 53 CLLocationCoordinate2D coordinate2D = location.coordinate; 54 NSString* message = [NSString stringWithFormat:@"經度:%lf,維度是:%lf",coordinate2D.longitude,coordinate2D.latitude]; 55 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"顯示當前位置的經緯度"
message:message delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil]; 56 [alertView show]; 57 58 } 59 } 60 61 @end
轉載請注明出處:http://www.cnblogs.com/goodboy-heyang/p/5161989.html