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

來源:互聯網
上載者:User

前言:關於地理位置及定位系統,在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入門筆記之地理位置定位系統,希望對大家有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.