2017-06-03 165 views
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描绘路径?

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

问候。

+1

下面是一些类似答案中的Objective-C代码,应该可以帮助你。 https://stackoverflow.com/a/23818032/3704795 – StevenOjo

+0

@StevenOjo:谢谢你的回复 – user3804063

+0

@StevenOjo:我可以制定一条路线并解决问题,请在答案中写下我将upvote并接受你。 – user3804063

回答