Pocket Note can be shown current place on map.I introduce how to get current place this time.
環境[Environment]:Xcode 8, Swift 3
まず地図を表示する為にMapKitをImportし、MkMapViewを貼り付けます。
At first,Import MapKit and add MkMapView for showing map.
import MapKit
・・・・・・・・
let mkMapView:MKMapView = MKMapView()
次に現在地を取得する為にCoreLocationをimportし、CLLocationManagerを宣言します。
またCLLocationManagerのイベントを受け取る為にCLLocationManagerDelegateを継承します。
Next,import CoreLocation and declare CLLocationManager.
And inherit CLLocationManagerDelegate for receiving events of CLLocationManager.
import CoreLocation
・・・・・・・・
var locationManager: CLLocationManager?
現在地を取得するにはまず、CLLocationManagerのrequestAlwaysAuthorizationメソッドでユーザーから現在地情報を取得する許可を得る必要があります。
Execute requestAlwaysAuthorization of CLLocationManager for getting user's permission to access to current place information.
locationManager = CLLocationManager()
locationManager!.requestAlwaysAuthorization()
この為に、info.plistにNSLocationAlwaysUsageキーを追加し、現在地情報にアクセスする理由を記載します。
And write NSLocationAlwaysUsage key to info.plist and write reason of accessing to current place information.
これにより現在地情報にアクセスする時に次のようなメッセージがユーザーに表示されます。(画像は言語の設定が英語の場合です。)
And display below message when an application access to current place information.
ユーザーから現在地情報にアクセスする許可を得られたらCLLocationManagerのstartUpdateLocationメソッドで現在地を取得します。
Execute startUpdateLocation of CLLocationManager for getting current place after getting user's permission to access to current place information.
if CLLocationManager.locationServicesEnabled(){
locationManager!.startUpdatingLocation()
}
現在地が取得できたら、CLLocationManagerのdidUpdateLocationsイベントが呼び出されますので、MKMapKitに位置情報を設定します。
この時、地図のズームを少し拡大するといいでしょう。
And if an application can get current place,didUpdateLocations of CLLocationManager is called.
And set current place to MKMapKit.
It is good to scale up on MKMapKit at this time.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//Get Details of Current Place
let location = locations[0]
let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
let newlocation = CLLocationCoordinate2DMake(latitude,longitude)
let span = MKCoordinateSpan(latitudeDelta: 0.005,longitudeDelta: 0.005)
let region = MKCoordinateRegion(center: newlocation,span: span)
//Show Current Place on Map
mkMapView.setRegion(region, animated: true)
locationManager!.stopUpdatingLocation()
locationManager = nil
mkMapView.showsUserLocation = false
}
あと現在地の取得が終わったらCLLocationManagerのstopUpdatingLocationメソッドを呼び出して、現在地を取得する処理を止めるようにしましょう。止め忘れると恐ろしい勢いでメモリ使用量が増えてしまいます。
You must call stopUpdatingLocation of CLLocationManager for stopping to get current place information.If you forget to call stopUpdatingLocation, memory size of an application is increasing quickly.
Let's see the full text of code.
ViewController.swift
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
let mkMapView:MKMapView = MKMapView()
let buttonCurrentPlace:UIButton = UIButton()
var locationManager: CLLocationManager?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
mkMapView.frame = CGRect(x:10,y:50,width:350,height:500)
self.view.addSubview(mkMapView)
buttonCurrentPlace.frame = CGRect(x:10,y:600,width:350,height:50)
buttonCurrentPlace.setTitle("Show Current Place", for: .normal)
buttonCurrentPlace.setTitleColor(UIColor.blue, for: .normal)
buttonCurrentPlace.titleLabel?.adjustsFontSizeToFitWidth = true
buttonCurrentPlace.addTarget(self, action: #selector(self.touchUpButtonCurrentPlace), for: .touchUpInside)
self.view.addSubview(buttonCurrentPlace)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Tap Show Current Place Button
func touchUpButtonCurrentPlace(){
locationManager = CLLocationManager()
locationManager!.requestAlwaysAuthorization()
locationManager!.delegate = self
if CLLocationManager.locationServicesEnabled(){
locationManager!.startUpdatingLocation()
}
}
//Get Current Place
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//Get Details of Current Place
let location = locations[0]
let latitude = location.coordinate.latitude
let longitude = location.coordinate.longitude
let newlocation = CLLocationCoordinate2DMake(latitude,longitude)
let span = MKCoordinateSpan(latitudeDelta: 0.005,longitudeDelta: 0.005)
let region = MKCoordinateRegion(center: newlocation,span: span)
//Show Current Place on Map
mkMapView.setRegion(region, animated: true)
locationManager!.stopUpdatingLocation()
locationManager = nil
mkMapView.showsUserLocation = false
}
}
[関連記事(Articles)]
にほんブログ村
クリエイティブライフ ブログランキングへ
0 件のコメント:
コメントを投稿