Something like a template to write to the controller when Map Kit View
is installed and its explanation.
It is assumed that there are ** Map Kit View ** and ** Label ** on one screen, and if you press and hold a specific part of the map, the address will be displayed in the Lebel part.
ViewController.swift
import UIKit
//The following two are required to use Map Kit View
import MapKit
import CoreLocation
//Declare two protocols
class ViewController: UIViewController,CLLocationManagerDelegate, UIGestureRecognizerDelegate {
//Define a variable that contains an address
var addressString = ""
//Recognize long press
@IBOutlet var longPress: UILongPressGestureRecognizer!
//Connect Map Kit View from the storyboard by pressing and holding the Control key
@IBOutlet weak var mapView: MKMapView!
//Create an instance to get location information
var locationManager:CLLocationManager!
//Connect Label from the storyboard by pressing and holding the Control key
@IBOutlet weak var addressLabel: UILabel!
//Not particularly relevant this time
override func viewDidLoad() {
super.viewDidLoad()
}
//Long Press Gesture Recognizer installed on the storyboard (see the bottom of this article for details)
@IBAction func longPressTap(_ sender: UILongPressGestureRecognizer) {
//When you start tapping
if sender.state == .began{
//When you finish tapping
} else if sender.state == .ended {
//Get the latitude and longitude of MKMapView by specifying the tapped position (CGPoint)
let tapPoint = sender.location(in: view)
let center = mapView.convert(tapPoint, toCoordinateFrom: mapView)
//latitude
let lat = center.latitude
//longitude
let log = center.longitude
//Pass arguments to the method that converts latitude / longitude to address
convert(lat: lat, log: log)
}
}
//Methods for converting latitude and longitude
//The properties written around here may be easier to understand by looking at the link at the bottom of this article
func convert(lat:CLLocationDegrees, log:CLLocationDegrees) {
//Convert from address to latitude / longitude
let geocoder = CLGeocoder()
//Create an address from latitude and longitude
let location = CLLocation(latitude: lat, longitude: log)
//Closure (In principle, write things inside the closure with self. The inside of the parentheses is called after the value is entered, and the outside of the parentheses is called until the value is entered)
//Get an address by reverse geocoding from longitude and latitude
geocoder.reverseGeocodeLocation(location) {
(placeMark, error) in
//If you can get the address you expected
if let placeMark = placeMark {
if let pm = placeMark.first {
//If you cannot get the address you expected, combine the character strings to create it.
if pm.administrativeArea != nil || pm.locality != nil {
self.addressString = pm.name! + pm.administrativeArea! + pm.locality!
} else {
self.addressString = pm.name!
}
//Show address in Labal
self.addressLabel.text = self.addressString
}
}
}
}
}
Long Press Gesture Recognizer
View Controller.swift
to connect it.reference
** I thought that the value that can be taken with the CLPlaceMark name changed in iOS 11, and verified it, and found that it was a strange property ** https://qiita.com/fr0g_fr0g/items/356d88ec906f2004f5f6
It's like this. With a vague understanding, it feels like a bit of a messy code.