iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 9
1
Software Development

30天Swift入門學習系列 第 9

iOS App實作(8) GPS定位-1 (Apple Map)


Description:
這demo會介紹如何將Apple map放到畫面中,另外還會說明如何開啟GPS定位,讓user可以看到目前在地圖上的自身位置。


Component:
1.CLLocationManager
2.MKMapView


Highlight function:
要使用apple地圖及定位功能時須添加額外library進project中,這邊會分別介紹如何使用地圖及定位功能。

import MapKit
import CoreLocation
  1. 定位功能的宣告及使用方法:
var myLocationMgr :CLLocationManager!
myLocationMgr = CLLocationManager()
myLocationMgr.distanceFilter = kCLLocationAccuracyNearestTenMeters 
myLocationMgr.desiredAccuracy = kCLLocationAccuracyBest

CLLocationManager中的distanceFilter property的目的是當使用者移動多少距離後會更新座標點。
desiredAccuracy property的目的是設定定位的精確度。
可以設置的值如下:
kCLLocationAccuracyBestForNavigation:精確度最高,適用於導航的定位。
kCLLocationAccuracyBest:精確度高。
kCLLocationAccuracyNearestTenMeters:精確度 10 公尺以內。
kCLLocationAccuracyHundredMeters:精確度 100 公尺以內。
kCLLocationAccuracyKilometer:精確度 1 公里以內。
kCLLocationAccuracyThreeKilometers:精確度 3 公里以內。

在viewDidAppear()中加入取得定位權限,讓每次進到app後都會先確認權限是否已取得。取得方式是透過CLLocationManager authorizationStatus()來進行。

// Get user author 
switch CLLocationManager.authorizationStatus() { 
case .notDetermined: 
  myLocationMgr.requestWhenInUseAuthorization() // First time lanch app need to get authorize from user 
  fallthrough 
case .authorizedWhenInUse: 
  myLocationMgr.startUpdatingLocation() // Start location 
case .denied: 
  let alertController = UIAlertController(title: "定位權限已關閉", message:"如要變更權限,請至 設定 > 隱私權 > 定位服務 開啟", preferredStyle: .alert) 
  let okAction = UIAlertAction(title: "確認", style: .default, handler:nil) 
  alertController.addAction(okAction) 
  self.present(alertController, animated: true, completion: nil) 
default: 
  break 
}

定位的開始與結束:

myLocationMgr.startUpdatingLocation()// Start location
myLocationMgr.stopUpdatingLocation()// Stop location 
  1. Apple map view宣告及使用方法:
var myMapView :MKMapView!
myMapView = MKMapView()
myMapView.mapType = .standard
myMapView.showsUserLocation = true
myMapView.isZoomEnabled = true

MKMapView中的mapType property,除了標準模式".standard",還可設置衛星模式".satellite"與混和模式".hybrid"等等。
showsUserLocation property則是可以在地圖上顯示定位,只要有允許定位權限,且此property也設為true時,地圖上就會出現自身定位位置。

最後透過CLLocationManagerDelegate來更新GPS位址:

let currentLocation: CLLocation = locations[0] as CLLocation
let center = CLLocationCoordinate2D(latitude: currentLocation.coordinate.latitude,
                                    longitude: currentLocation.coordinate.longitude)
    
// paramater span means the range of map
let region = MKCoordinateRegion(center: center, 
                                span: MKCoordinateSpan(latitudeDelta: 0.01,
                                                       longitudeDelta: 0.01)
                               )
myMapView.setRegion(region, animated: true)

Additional:
在plist.info中添加設定來向使用者取得定位權限的方法:


Reference:
Source code on Github


上一篇
iOS App實作(7) ActivitySchedule(UIDatePicker)
下一篇
iOS App 實作(9) GPS 定位-2(Google Map)
系列文
30天Swift入門學習30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言