I introduce how to show Pin on the place which is done with a long press of MKMapView this time.
[写真はPocket Note(This pic is Pocket Note)]
環境[Environment]:Xcode8, Swift3
まずは 長押しを検出するUILongPressGestureRecognizerを宣言します。
At first,declare UILongPressGestureRecognizer for detecting Long Press.
let longtapGesture = UILongPressGestureRecognizer(target:self, action:#selector(self.longPress(sender:)))
mkMapView.addGestureRecognizer(longtapGesture)
次に長押し後のイベントの中で、長押しされた位置を取得します。
Pinを立てる為に最終的にはCLLocationCoordinate2Dにします。
Next, get location of the place which is done with a long press in the event after a long press.
Get CLLocationCoordinate2D for showing Pin on the map.
let location = sender.location(in: mkMapView)
let locationCoordinate2D:CLLocationCoordinate2D = mkMapView.convert(location, toCoordinateFrom: mkMapView)
Next, make MKPointAnnotation, and set location to MkPointAnnotation, and add MKPointannotation to MKMapView.
let annotation = MKPointAnnotation()
annotation.coordinate = locationCoordinate2D
mkMapView.addAnnotation(annotation)
ではソースコード全文です。
Full text of code is below.
Full text of code is below.
ViewController.swift
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
let mkMapView:MKMapView = MKMapView()
let buttonCurrentPlace:UIButton = UIButton()
let buttonScaleUp:UIButton = UIButton()
let buttonScaleDown: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)
let longtapGesture = UILongPressGestureRecognizer(target:self, action:#selector(self.longPress(sender:)))
mkMapView.addGestureRecognizer(longtapGesture)
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)
buttonScaleUp.frame = CGRect(x:10,y:660,width:350,height:50)
buttonScaleUp.setTitle("Scale Up", for: .normal)
buttonScaleUp.setTitleColor(UIColor.blue, for: .normal)
buttonScaleUp.titleLabel?.adjustsFontSizeToFitWidth = true
buttonScaleUp.addTarget(self, action: #selector(self.touchUpButtonScaleUp), for: .touchUpInside)
self.view.addSubview(buttonScaleUp)
buttonScaleDown.frame = CGRect(x:10,y:720,width:350,height:50)
buttonScaleDown.setTitle("Scale Down", for: .normal)
buttonScaleDown.setTitleColor(UIColor.blue, for: .normal)
buttonScaleDown.titleLabel?.adjustsFontSizeToFitWidth = true
buttonScaleDown.addTarget(self, action: #selector(self.touchUpButtonScaleDown), for: .touchUpInside)
self.view.addSubview(buttonScaleDown)
}
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
}
//The scale of MapKitView is upped
func touchUpButtonScaleUp(){
var latitudeDelta = mkMapView.region.span.latitudeDelta
var longitudeDelta = mkMapView.region.span.longitudeDelta
latitudeDelta = latitudeDelta/5
if (latitudeDelta < 0.0001){
latitudeDelta = 0.0001
}
longitudeDelta = longitudeDelta/5
if (longitudeDelta < 0.0001){
longitudeDelta = 0.0001
}
let span = MKCoordinateSpan(latitudeDelta: latitudeDelta,longitudeDelta: longitudeDelta)
let region = MKCoordinateRegion(center: mkMapView.region.center,span: span)
mkMapView.setRegion(region, animated: true)
}
//The scale of MapKitView is downed
func touchUpButtonScaleDown(){
var latitudeDelta = mkMapView.region.span.latitudeDelta
var longitudeDelta = mkMapView.region.span.longitudeDelta
latitudeDelta = latitudeDelta*5
if (latitudeDelta > 27.77){
latitudeDelta = 27.77
}
longitudeDelta = longitudeDelta*5
if (longitudeDelta > 16.69){
longitudeDelta = 16.69
}
let span = MKCoordinateSpan(latitudeDelta: latitudeDelta,longitudeDelta: longitudeDelta)
let region = MKCoordinateRegion(center: mkMapView.region.center,span: span)
mkMapView.setRegion(region, animated: true)
}
//Detect Long Press
func longPress(sender: UILongPressGestureRecognizer){
if(sender.state != .began){
return
}
//Get Location
let location = sender.location(in: mkMapView)
let locationCoordinate2D:CLLocationCoordinate2D = mkMapView.convert(location, toCoordinateFrom: mkMapView)
//Create Pin
let annotation = MKPointAnnotation()
annotation.coordinate = locationCoordinate2D
//Delete Existing Pin
if (mkMapView.annotations.count > 0){
mkMapView.removeAnnotations(mkMapView.annotations)
}
//Show Pin on Map
mkMapView.addAnnotation(annotation)
}
}
[関連記事(Articles)]
[iOS]How to show current place on Map
にほんブログ村
クリエイティブライフ ブログランキングへ
0 件のコメント:
コメントを投稿