2017-06-03 2 views
0

Ich habe (lat, lang) von allen Punkten, die Benutzer gereist ist.iOS Swift - Zeichnen Sie Route mit mehreren Koordinaten in MKMapView

Apple Mapkit gibt mir die Möglichkeit, Route zwischen Startort und Zielort zu zeichnen.

Aber ich möchte meine alle Punkte, die Benutzer gereist ist, einschließen.

Im Folgenden finden Sie den Code zum Zeichnen der Route zwischen Anfangs- und Endpositionen.

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) 
        } 

Meine Frage ist: Gibt es eine Möglichkeit, dass ich meine anderen Punkte umfassen kann, während Route in der MapKit Zeichnung?

Alle Vorschläge, Links .... sehr geschätzt.

Grüße.

+1

Hier ist einige Objective-C-Code in einer ähnlichen Antwort, die Ihnen auf Ihrem Weg helfen sollte. https://StackOverflow.com/a/23818032/3704795 – StevenOjo

+0

@StevenOjo: danke für die Antwort – user3804063

+0

@StevenOjo: Ich kann eine Route machen und das Problem lösen, bitte schreibe in Antworten, ich werde upvote und Sie akzeptieren. – user3804063

Antwort