获取地图视图上的图钉方向

时间:2018-06-05 06:23:42

标签: ios swift direction

现在我在iOS编码地图,并试图找出如何获取我编码的每个在地图上永久设置的引脚的路线。

例如,我想要我的搜索栏做的是当我输入我放置的那些永久别针之一的名称时,搜索栏会识别出直接转到我放置的那个引脚。

我需要知道如何找到我放在地图上的引脚。我点击了针脚,但是我没有从当前位置获取方向。这就是我想要弄明白的东西。

这就是我到目前为止所放置的针脚:

let location11 = CLLocationCoordinate2D( latitude: 33.232233, longitude: -111.232322)

let span11 =  MKCoordinateSpanMake(0.02, 0.02)

let region11 = MKCoordinateRegion(center: location11, span: span11);

mapView.setRegion(region11, animated: true)

let Annotation10 = MKPointAnnotation()
Annotation10.coordinate = location11
Annotation10.title = " blahhh "
Annotation10.subtitle = "Campus"

mapView.addAnnotation (Annotation10)

以下是我设置searchBar委托的方式:

extension ViewController: UISearchBarDelegate {
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.endEditing(true)
        let localSearchRequest = MKLocalSearchRequest()
        localSearchRequest.naturalLanguageQuery = searchBar.text
        let region = MKCoordinateRegion(center: currentCoordinate, span: MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002))
        localSearchRequest.region = region
        let localSearch = MKLocalSearch(request: localSearchRequest)
        localSearch.start { (response, _) in
            guard let response = response else { return }
            guard let firstMapItem = response.mapItems.first else
            { return }
            self.getDirections(to: firstMapItem)
        }
    }

这是我的地图视图委托:

extension ViewController: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        if overlay is MKPolyline {
            let renderer = MKPolylineRenderer(overlay: overlay)
            renderer.strokeColor = .yellow
            renderer.lineWidth = 5
            return renderer
        }
        if overlay is MKCircle {
            let renderer = MKCircleRenderer(overlay: overlay)
            renderer.strokeColor = .red
            renderer.fillColor = .red
            renderer.alpha = 0.005
            return renderer
        }
        return MKOverlayPathRenderer()
    }
}

1 个答案:

答案 0 :(得分:0)

首先将mkmapview变量委托给您的父视图/ viewcontroller。 获取所选引脚有一种委托方法 首先使用MKMapView委托方法在地图上添加带有适当信息的图钉:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")//Add your information here to the pin....like title,tag,color...}

然后点击图钉视图,将调用此方法....获取图钉并提取路线计算所需的信息

  func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {//Get location and other info from view}