iOS Swift - 使用MKMapView中的多个坐标绘制路线

时间:2017-06-03 20:48:01

标签: ios swift3 routing mapkit

我得到了(lat,long)用户所经历过的所有积分。

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中绘制路线时,有什么办法可以包含我的其他要点吗?

任何建议,链接....高度赞赏。

问候。

1 个答案:

答案 0 :(得分:2)

以下是类似答案中的一些Objective-C代码,可以帮助您。

https://stackoverflow.com/a/23818032/3704795

MapKit是一个非常强大的API!