【问题标题】:MapKit - Route is found but not displayingMapKit - 找到路线但未显示
【发布时间】:2019-08-15 06:53:40
【问题描述】:

我正在 iPhone 模拟器中创建一条从用户当前位置到另一点的路线。路线创建得很好,我能够掌握前往目的地所需的时间和距离,但路线折线/叠加层没有显示。代码很大,所以下面是我认为解释问题所需的基本部分。

func tapTocreateRoute(){
    location = CLLocationCoordinate2D(latitude: 37.787359, longitude: -122.41)
    let placemark = MKPlacemark(coordinate: location)
    let mapItem = MKMapItem(placemark: placemark)
    routeCreater(mapItem: mapItem)      
}

 func routeCreater(mapItem: MKMapItem){

    let destinationPin = customPin(pinTitle: "end", pinSubtitle: "testing", location: carLocation)
    self.mapView.addAnnotation(destinationPin)

    let request = MKDirections.Request()
    request.source = MKMapItem.forCurrentLocation()
    request.destination = mapItem
    request.transportType = MKDirectionsTransportType.walking

    let directions = MKDirections(request: request)

    directions.calculate { (response, error) in
        guard let response = response else {
            print("MKDIRECTION error: \(error?.localizedDescription ?? "Error not found")")
            return
        }

        self.route = response.routes[0]
        self.mapView.addOverlay(self.route.polyline, level: .aboveRoads)

        self.mapView.setVisibleMapRect(self.route.polyline.boundingMapRect, animated: true)
        let rect = self.route.polyline.boundingMapRect
        self.mapView.setRegion(MKCoordinateRegion(rect), animated: true)

        self.walkingTime =  self.route.expectedTravelTime //displays correctly
        self.walkingDistance = self.route.distance // displays correctly

    }
}

  //Line Renderer for Route
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let lineRenderer = MKPolylineRenderer(overlay: overlay)
    lineRenderer.strokeColor = UIColor(red: 216/255, green: 71/255, blue: 30/255, alpha: 1)
    lineRenderer.lineWidth = 3.0
    return lineRenderer
}

我在渲染中遗漏了什么吗? 感谢您的帮助

【问题讨论】:

  • 您尚未将生成的折线添加到地图视图中。
  • 抱歉,El Tomato 到底是什么意思?我正在根据路线将叠加层添加到地图中。

标签: swift xcode mapkit core-location mkoverlay


【解决方案1】:

您是否确保分配 MapView 的委托,以便它知道在哪里寻找渲染器?

【讨论】:

  • 嗨,Flowinho,是的,我愿意
  • 我以编程方式创建了整个视图,并在创建地图视图之前调用了 MapView 委托。感谢您让我意识到我的逻辑错误!现在可以了!
猜你喜欢
  • 1970-01-01
  • 2020-03-05
  • 2019-08-14
  • 2010-11-26
  • 2015-06-16
  • 2021-02-24
  • 2019-02-11
  • 2020-04-05
  • 1970-01-01
相关资源
最近更新 更多