我正在开发一个需要绘制路径的应用程序,以便用户在Apple Maps上行驶。我以绘制的路径应经过的经度和纬度数组的形式获取用户访问的点。 我是Google地图的新手,所以请帮帮我。
我有经度和纬度,但是当我第一次尝试创建路径时,它直接采用了从A点到B点的路径,而没有考虑中间点。 当我尝试另一种方法时,考虑了中间点,但这些线是完全笔直的(从A点到中间点,然后再笔直到B点,而忽略了建筑物和道路。)
我尝试过的代码: 方法1:
func showRouteOnMapForJourneys(viaPoints: [ViaPointCoordinatesModel] ,startPoints: StartingPointCoordinatesModel, endPoints: EndingPointCoordinatesModel) {
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: startPoints.latitude, longitude: startPoints.longitude), span: span)
myMapView.setRegion(region, animated: true)
myMapView.delegate = self
let sourceLocation = CLLocationCoordinate2D(latitude: startPoints.latitude, longitude: startPoints.longitude)
let destinationLocation = CLLocationCoordinate2D(latitude: endPoints.latitude, longitude: endPoints.longitude)
let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
let sourceAnnotation = MKPointAnnotation()
sourceAnnotation.title = NSLocalizedString("Start Location", comment: "")
if let location = sourcePlacemark.location {
sourceAnnotation.coordinate = location.coordinate
}
let destinationAnnotation = MKPointAnnotation()
destinationAnnotation.title = NSLocalizedString("End Location", comment: "")
if let location = destinationPlacemark.location {
destinationAnnotation.coordinate = location.coordinate
}
self.myMapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true)
let start = CLLocationCoordinate2D(latitude: startPoints.latitude, longitude: startPoints.longitude)
locations.append(start)
for coords in viaPoints {
let lat = Double(coords.latitude)
let lon = Double(coords.longitude)
let destination = CLLocationCoordinate2D(latitude: lat, longitude: lon)
locations.append(destination)
}
let end = CLLocationCoordinate2D(latitude: endPoints.latitude, longitude: endPoints.longitude)
locations.append(end)
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
let route = response.routes[0]
self.myMapView.add((route.polyline), level: MKOverlayLevel.aboveRoads)
}
}
方法2:
func addPolyLine(viaPoints: [ViaPointCoordinatesModel], startLatitude: Double, startLongitude: Double, endLatitude: Double, endLongitude: Double){
locations.removeAll()
myMapView?.delegate = self
let sourceLocation = CLLocationCoordinate2D(latitude: startLatitude, longitude: startLongitude)
let destinationLocation = CLLocationCoordinate2D(latitude: endLatitude, longitude: endLongitude)
let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
let sourceAnnotation = MKPointAnnotation()
sourceAnnotation.title = NSLocalizedString("Start Location", comment: "")
if let location = sourcePlacemark.location {
sourceAnnotation.coordinate = location.coordinate
}
let destinationAnnotation = MKPointAnnotation()
destinationAnnotation.title = NSLocalizedString("End Location", comment: "")
if let location = destinationPlacemark.location {
destinationAnnotation.coordinate = location.coordinate
}
self.myMapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true)
let start = CLLocationCoordinate2D(latitude: startLatitude, longitude: startLongitude)
locations.append(start)
for coords in viaPoints {
let lat = Double(coords.latitude)
let lon = Double(coords.longitude)
let destination = CLLocationCoordinate2D(latitude: lat, longitude: lon)
locations.append(destination)
}
let end = CLLocationCoordinate2D(latitude: endLatitude, longitude: endLongitude)
locations.append(end)
let geodesic = MKGeodesicPolyline(coordinates: locations, count: locations.count)
myMapView.add(geodesic)
UIView.animate(withDuration: 1.5, animations: { () -> Void in
let span = MKCoordinateSpanMake(0.05, 0.05)
let region1 = MKCoordinateRegion(center: self.locations[0], span: span)
self.myMapView.setRegion(region1, animated: true)
})
}
线条要么忽略了中间点,要么忽略了道路