0
我得到了(lat,long)用戶所走過的所有點。iOS Swift - 在MKMapView中使用多個座標繪製路線
Apple Mapkit爲我提供了在開始位置和目的地位置之間繪製路線的方法。
但我想包括我所有的用戶旅行的觀點。
以下是在開始和結束位置之間繪製路線的代碼。
let startloc = todaysLocationList.first
let endloc = todaysLocationList.last
// 2.
let sourceLocation = CLLocationCoordinate2D(latitude: (startloc?.latitude)!, longitude: (startloc?.longitude)!)
let destinationLocation = CLLocationCoordinate2D(latitude: (endloc?.latitude)!, longitude: (endloc?.longitude)!)
// 3.
let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
// 4.
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
// 5.
let sourceAnnotation = MKPointAnnotation()
sourceAnnotation.title = "Times Square"
if let location = sourcePlacemark.location {
sourceAnnotation.coordinate = location.coordinate
}
let destinationAnnotation = MKPointAnnotation()
destinationAnnotation.title = "Empire State Building"
if let location = destinationPlacemark.location {
destinationAnnotation.coordinate = location.coordinate
}
// 6.
mapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true)
// 7.
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .automobile
// Calculate the direction
let directions = MKDirections(request: directionRequest)
// 8.
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
let route = response.routes[0]
mapView.add((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect = route.polyline.boundingMapRect
mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
}
我的問題是: 有什麼辦法,我可以包括我的其他點,而在mapkit描繪路徑?
任何建議,鏈接....高度讚賞。
問候。
下面是一些類似答案中的Objective-C代碼,應該可以幫助你。 https://stackoverflow.com/a/23818032/3704795 – StevenOjo
@StevenOjo:謝謝你的回覆 – user3804063
@StevenOjo:我可以制定一條路線並解決問題,請在答案中寫下我將upvote並接受你。 – user3804063