我为源和目的地传递了两个纬度和经度,但是没有在两个点之间绘制折线。总是出现错误错误。下面的代码我在源坐标中传递了当前位置,在destCoordinates中传递了行驶位置。我想在sourceCoordinates和destCoordinates之间画一条折线。
if(CLLocationManager.locationServicesEnabled()){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
//let sourceCoordinates = locationManager.location?.coordinate
let sourceCoordinates = CLLocationCoordinate2DMake(19.115950,72.856448)
let destCoordinates = CLLocationCoordinate2DMake(19.119670,72.853616)
let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates)
let destPlacemark = MKPlacemark(coordinate: destCoordinates)
let sourceItem = MKMapItem(placemark: sourcePlacemark)
let destItem = MKMapItem(placemark: destPlacemark)
let directionRequest = MKDirectionsRequest()
directionRequest.source=sourceItem
directionRequest.destination=destItem
directionRequest.transportType = .walking
let directions = MKDirections(request: directionRequest)
directions.calculate(completionHandler: {
response,error in
guard let response = response else{
if let error = error {
print("something wrong")
}
return
}
let route = response.routes[0]
self.upcoming_info_map.add(route.polyline,level: .aboveRoads)
let rekt = route.polyline.boundingMapRect
self.upcoming_info_map.setRegion(MKCoordinateRegionForMapRect(rekt), animated: true)
})
对于绘制折线,我使用此功能
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolygonRenderer(overlay:overlay)
renderer.strokeColor = UIColor.blue
renderer.lineWidth = 5.0
return renderer
}
答案 0 :(得分:0)
使用此方法。获取位置列表并绘制折线
func addPolyLineToMap(googlemaplist: [CLLocation?]){
var coordinates = googlemaplist.map({ (location: CLLocation!) -> CLLocationCoordinate2D in
return location.coordinate
})
print("locatios count")
print(googlemaplist.count)
var polyline = MKPolyline(coordinates: &coordinates, count: googlemaplist.count)
DispatchQueue.main.async {
self.MapKit.add(polyline)
}
}