1

I got (lat,long) of all the points user has travelled.

Apple Mapkit gives me the way to draw route between start location and destination location.

But I want to include my all the points that user has travelled.

Following is the code to draw route between start and end locations.

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

My question is: Is there any way that I can include my other points while drawing route in the mapkit ?

Any suggestions, links....highly appreciated.

Regards.

user3804063
  • 809
  • 1
  • 14
  • 32

0 Answers0