【问题标题】:Mapkit get current coordinates of placemarkerMapkit 获取地标的当前坐标
【发布时间】:2019-03-21 02:54:38
【问题描述】:

我对 MapKit 还很陌生,一旦选择了地点标记,我就会尝试显示信息和方向。我正在展示当地医院和紧急服务。如何获取当前选择的地标的信息。我希望能够显示有关所选标记的几行信息。例如姓名、地址、电话号码,可能还有一个方向按钮。我想将当前选择的标记坐标保存到一个变量中。

maps image

class MapKitViewController: UIViewController, MKMapViewDelegate {


let locationManager = CLLocationManager()
let regionInMeters: Double = 10000
var previousLocation: CLLocation?

let geoCoder = CLGeocoder()
var directionsArray: [MKDirections] = []



func setupLocationManager() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
}

func centerViewOnUserLocation() {
    if let location = locationManager.location?.coordinate {
        let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
        mapView.setRegion(region, animated: true)
    }
}


func checkLocationServices() {
    if CLLocationManager.locationServicesEnabled() {
        setupLocationManager()
        performSearch()
        checkLocationAuthorization()
    } else {
        // Show alert letting the user know they have to turn this on.
    }
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation)
    -> MKAnnotationView? {

        let identifier = "marker"
        var view: MKMarkerAnnotationView

        if let dequeuedView = mapView.dequeueReusableAnnotationView(
            withIdentifier: identifier)
            as? MKMarkerAnnotationView {
            dequeuedView.annotation = annotation
            view = dequeuedView
        } else {
            view =
                MKMarkerAnnotationView(annotation: annotation,
                                       reuseIdentifier: identifier)


            view.markerTintColor = UIColor.blue
            view.canShowCallout = true
            view.calloutOffset = CGPoint(x: -5, y: 5)
            view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)

        }
        return view
}

func mapView(_: MKMapView, annotationView:
    MKAnnotationView, calloutAccessoryControlTapped: UIControl) {


    print("Control tapped")
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {


}

【问题讨论】:

  • 您所说的“地标”是什么?它是注释标记还是地标?
  • 我的意思是图像中的红色大头针,结果显示在地图上。

标签: swift mapkit mapkitannotation


【解决方案1】:

原来的注解类(MKPointAnnotation)除了标题和副标题外,不允许你给pin提供太多信息。所以你需要从MKPointAnnotation中创建一个子类,这样你就可以唯一地识别用户点击的pin,这将被MKMapView的didSelect委托方法检测到。

首先,创建一个名为 MyPointAnnotation 的 swift 文件,如下所示。

import MapKit

class MyPointAnnotation: MKPointAnnotation {
    var identifier: String?
    //var image: UIImage?
    var lat: Double!
    var lon: Double!
}

然后使用上面的子类进行标注,添加到地图中。

import UIKit
import MapKit

class MapViewController: UIViewController, MKMapViewDelegate {
    // MARK: - Variables

    // MARK: - IBOutlet
    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.delegate = self
    }

    @IBAction func addNewPin(_ sender: UIBarButtonItem) {
        addNewAnnotationPin(title: "Restaurant", subTitle: "Barbecue", lat: 35.6387264874361, lon: 139.99950350000003)
    }

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !(annotation is MKUserLocation) else {
            return nil
        }

        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView?.pinTintColor = UIColor.red
            pinView?.canShowCallout = true
        }
        else {
            pinView?.annotation = annotation
        }
        return pinView
    }

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        if view.annotation is MyPointAnnotation {
            if let selectedAnnotation = view.annotation as? MyPointAnnotation {
                if let id = selectedAnnotation.identifier {
                    for pin in mapView.annotations as! [MyPointAnnotation] {
                        if let myIdentifier = pin.identifier {
                            if myIdentifier == id {
                                print(pin.lat ?? 0.0)
                                print(pin.lon ?? 0.0)
                            }
                        }
                    }
                }
            }
        }
    }

    func addNewAnnotationPin(title: String, subTitle: String, lat: Double, lon: Double) {
        let myPin = MyPointAnnotation()
        myPin.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
        myPin.title = title
        myPin.subtitle = subTitle
        myPin.identifier = UUID().uuidString
        myPin.lat = lat
        myPin.lon = lon
        self.mapView.addAnnotation(myPin)
    }
}

【讨论】:

  • addNewPin 动作从何而来?
  • 您不必使用它。您可以以任何您想要的方式添加注释图钉。我创建它是为了方便。
  • 我已经尝试过了,但选择后控制台中没有打印任何内容
  • 我自己测试过。如果它不起作用,请不要怪我。
  • 不,我没有责怪你,它非常有帮助,我正在尝试更多地测试它,谢谢你的帮助
猜你喜欢
  • 1970-01-01
  • 2010-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多