2点を確認してください:
0. ViewControllerはMKMapViewDelegate
を継承するか
0. self.mapView.delegate = self
- Delegateを設定しましたか。
動いているコード(カスタムアノテーションビューのサンプル)
swift
1//
2// MapKitViewController.swift
3// mapApp
4//
5// Created by リヴォーフ ユーリ on 2019/12/17.
6// Copyright © 2019 リヴォーフ ユーリ. All rights reserved.
7//
8
9import UIKit
10import MapKit
11
12class MapKitViewController: UIViewController, MKMapViewDelegate {
13
14 @IBOutlet weak var mapView: MKMapView!
15 var placemark: CLPlacemark? = nil
16 override func viewDidLoad() {
17 super.viewDidLoad()
18
19 let pin = UILongPressGestureRecognizer(target: self, action: #selector(addAnnotation))
20 pin.minimumPressDuration = 1.0
21 mapView.addGestureRecognizer(pin)
22 self.mapView.delegate = self
23 }
24
25 @objc func addAnnotation(gestureRecognizer:UIGestureRecognizer){
26 if gestureRecognizer.state == UIGestureRecognizer.State.began {
27 let touchPoint = gestureRecognizer.location(in: mapView)
28 let newCoordinates = mapView.convert(touchPoint, toCoordinateFrom: mapView)
29 let annotation = MKPointAnnotation()
30 annotation.coordinate = newCoordinates
31
32 CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: newCoordinates.latitude, longitude: newCoordinates.longitude), completionHandler: {(placemarks, error) -> Void in
33 if error != nil {
34 print("Reverse geocoder failed with error" + error!.localizedDescription)
35 return
36 }
37
38 if placemarks!.count > 0 {
39 self.placemark = placemarks![0]
40 self.mapView.addAnnotation(annotation)
41 }
42 else {
43 annotation.title = "Unknown Place"
44 self.mapView.addAnnotation(annotation)
45 print("Problem with the data received from geocoder")
46 }
47 }
48 )
49 }
50 }
51
52 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
53 // Don't want to show a custom image if the annotation is the user's location.
54 guard !(annotation is MKUserLocation) else {
55 return nil
56 }
57
58 // Better to make this class property
59 let annotationIdentifier = "AnnotationIdentifier"
60
61 var annotationView: MKAnnotationView?
62 if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
63 annotationView = dequeuedAnnotationView
64 annotationView?.annotation = annotation
65 }
66 else {
67 let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
68 av.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
69 annotationView = av
70 }
71
72 if let annotationView = annotationView {
73 // Configure your annotation view here
74 annotationView.canShowCallout = true
75 let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20));
76 imageView.image = UIImage(named: "lovedeathrobots")
77 annotationView.addSubview(imageView)
78
79 let label = UILabel(frame: CGRect(x: 0, y: 10, width: 100, height: 30))
80 label.textColor = .black
81 label.adjustsFontSizeToFitWidth = true
82 label.alpha = 0.8
83 label.text = (placemark?.thoroughfare ?? "") + " " + (placemark?.subThoroughfare ?? "")
84 annotationView.addSubview(label)
85 }
86
87 return annotationView
88 }
89}
90
デモアプリ:
https://github.com/vanderlvoff/mapApp
アプリを実行したら、
を押して、MapKitの画面に行けます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/23 17:37