Je veux ajouter des annotations sur le toucher de la carte iOS et récupérer l'adresse détaillée (repère) de l'emplacement respectif. Comment puis-je atteindre cet objectif dans Swift?
Merci d'avance.
Pour réagir au toucher sur la carte, vous devez configurer un dispositif de reconnaissance de frappe pour la carte.
en viewDidLoad
:
let gestureRecognizer = UITapGestureRecognizer(target: self, action:"handleTap:")
gestureRecognizer.delegate = self
mapView.addGestureRecognizer(gestureRecognizer)
Manipulez le robinet et obtenez les coordonnées de l'emplacement tapé:
func handleTap(gestureReconizer: UILongPressGestureRecognizer) {
let location = gestureReconizer.locationInView(mapView)
let coordinate = mapView.convertPoint(location,toCoordinateFromView: mapView)
// Add annotation:
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
mapView.addAnnotation(annotation)
}
Il ne vous reste plus qu'à implémenter les fonctions de délégué MKMapView pour dessiner les annotations. Une simple recherche sur Google devrait vous permettre d'obtenir le reste.
Swift 4:
@IBOutlet weak var mapView: MKMapView!
func handleLongPress (gestureRecognizer: UILongPressGestureRecognizer) {
if gestureRecognizer.state == UIGestureRecognizerState.began {
let touchPoint: CGPoint = gestureRecognizer.location(in: mapView)
let newCoordinate: CLLocationCoordinate2D = mapView.convert(touchPoint, toCoordinateFrom: mapView)
addAnnotationOnLocation(pointedCoordinate: newCoordinate)
}
}
func addAnnotationOnLocation(pointedCoordinate: CLLocationCoordinate2D {
let annotation = MKPointAnnotation()
annotation.coordinate = pointedCoordinate
annotation.title = "Loading..."
annotation.subtitle = "Loading..."
mapView.addAnnotation(annotation)
}
Voici un projet fonctionnel Xcode 10.1, Swift 4.2 avec des délégués MapKit pour contrôler les annotations (couleur de la broche, image de la broche, etc.) et un délégué pour gérer un clic sur l'annotation ajoutée: Projet Github
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
let longTapGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap))
mapView.addGestureRecognizer(longTapGesture)
}
@objc func longTap(sender: UIGestureRecognizer){
print("long tap")
if sender.state == .began {
let locationInView = sender.location(in: mapView)
let locationOnMap = mapView.convert(locationInView, toCoordinateFrom: mapView)
addAnnotation(location: locationOnMap)
}
}
func addAnnotation(location: CLLocationCoordinate2D){
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Some Title"
annotation.subtitle = "Some Subtitle"
self.mapView.addAnnotation(annotation)
}
}
extension ViewController: MKMapViewDelegate{
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is MKPointAnnotation else { print("no mkpointannotaions"); return nil }
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.rightCalloutAccessoryView = UIButton(type: .infoDark)
pinView!.pinTintColor = UIColor.black
}
else {
pinView!.annotation = annotation
}
return pinView
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
print("tapped on pin ")
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView {
if let doSomething = view.annotation?.title! {
print("do something")
}
}
}
}
Pour Swift 4 j'ai converti l'exemple de Swift 3 car l'autre Swift 4 ne fonctionnait pas pour moi:
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
gestureRecognizer.delegate = self
mapview.addGestureRecognizer(gestureRecognizer)
@objc func handleTap(_ gestureReconizer: UILongPressGestureRecognizer)
{
let location = gestureReconizer.location(in: mapview)
let coordinate = mapview.convert(location,toCoordinateFrom: mapview)
// Add annotation:
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
mapview.addAnnotation(annotation)
}
Pour Swift 3.0
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
gestureRecognizer.delegate = self
mapView.addGestureRecognizer(gestureRecognizer)
func handleTap(_ gestureReconizer: UILongPressGestureRecognizer) {
let location = gestureReconizer.locationInView(mapView)
let coordinate = mapView.convertPoint(location,toCoordinateFromView: mapView)
// Add annotation:
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
mapView.addAnnotation(annotation)
}