pass array of map coordinates to a draw a route on map in swift

大兔子大兔子 提交于 2021-02-11 12:41:44

问题


hi I have an array of coordinates data like this, which is retrieved from an API call, and I use a for loop to append the data into an array like this:

extension TripListViewController: UITableViewDelegate {


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        performSegue(withIdentifier: Constants.segueTripListToTripMap, sender: indexPath)

        tableView.deselectRow(at: indexPath, animated: true)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == Constants.segueTripListToTripMap {
            if let destinationVC = segue.destination as? TripMapViewController,
                let indexPath = sender as? IndexPath {
                
                
                for i in 0...(tripList[indexPath.section][indexPath.row].coordinates.count-1) {
                    self.coodinates.append(tripList[indexPath.section][indexPath.row].coordinates[i])
                }
                
                destinationVC.coodinatePoints = coodinates
                
                
                 
            }
        } else {
            if let destinationVC = segue.destination as? PolicyOverviewViewController,
                let indexPath = sender as? IndexPath {
                // do something
            }
        }

    }

}

it has an error... I don't know how to pass this array to the variable I declared in another screen, the data type seem doesn't match.

The original data type look like this:

struct Trips: Codable {
    let id: String
    let userId: String
    let coordinates: [Coordinates]

}

struct Coordinates: Codable {
    let lat: Double?
    let lng: Double?
    let time: String?
}

[
["time": "timestampes"
"lat": 40.213
"lon": 5.203],
["time": "timestampes"
"lat": 40.213
"lon": 5.203],
["time": "timestampes"
"lat": 40.213
"lon": 5.203],
["time": "timestampes"
"lat": 40.213
"lon": 5.203]

]

how can I pass this data into my draw route function's point variable. it's currently hardcoded with some dummy data.

func drawRoutes() {

        var points = [CLLocationCoordinate2DMake(51.079980, 4.349850),
                      CLLocationCoordinate2DMake(51.079060, 4.350830),
                      CLLocationCoordinate2DMake(51.078210, 4.350490),
                      CLLocationCoordinate2DMake(51.077750, 4.350890),
                      CLLocationCoordinate2DMake(51.076760, 4.354600),
                      CLLocationCoordinate2DMake(51.075130, 4.351000),
                      CLLocationCoordinate2DMake(51.073800, 4.350690),
                      CLLocationCoordinate2DMake(52.071850, 4.352880),
                      CLLocationCoordinate2DMake(52.069320, 4.355940),
                      CLLocationCoordinate2DMake(52.069120, 4.356130),
                      CLLocationCoordinate2DMake(52.069120, 4.356130),
                      CLLocationCoordinate2DMake(52.069120, 4.356130),
                      CLLocationCoordinate2DMake(52.068570, 4.356950),
                      CLLocationCoordinate2DMake(52.067840, 4.358440),
                      CLLocationCoordinate2DMake(52.066730, 4.357490),
                      CLLocationCoordinate2DMake(52.066590, 4.358680),
                      CLLocationCoordinate2DMake(52.066580, 4.358680),
                      CLLocationCoordinate2DMake(52.066580, 4.358680),
                      CLLocationCoordinate2DMake(52.066830, 4.357490),
                      CLLocationCoordinate2DMake(52.067600, 4.358520),
                      CLLocationCoordinate2DMake(52.068650, 4.356920),
                      CLLocationCoordinate2DMake(52.074330, 4.350360),
                      CLLocationCoordinate2DMake(52.075520, 4.351880),
                      CLLocationCoordinate2DMake(52.076950, 4.355350),
                      CLLocationCoordinate2DMake(52.078000, 4.350690),
                      CLLocationCoordinate2DMake(52.078010, 4.350710),
                      CLLocationCoordinate2DMake(52.079520, 4.351560),
                      CLLocationCoordinate2DMake(52.080680, 4.350220),
                      CLLocationCoordinate2DMake(52.080760, 4.348890),
                      CLLocationCoordinate2DMake(52.079890, 4.349980),
                      CLLocationCoordinate2DMake(52.079890, 4.350000)]

        let polygon = MKPolyline(coordinates: &points, count: points.count)

        self.mapView.addOverlay(polygon)
        self.mapView.setVisibleMapRect(polygon.boundingMapRect, animated: true)

        var startPoint = points[0]
        
        for i in 1...(points.count-1) {
            
            guard let request = createRequest(c1:startPoint, c2:points[i]) else { return }
            let directions = MKDirections(request: request)

            directions.calculate { [unowned self] (response, error) in
                guard let response = response else { return }
                let routes = response.routes
                let bestDest = routes[0]
                startPoint = points[i]
            }
        }
    }

thanks a lot !


回答1:


You can use map to convert your coordinates

let points: [CLLocationCoordinate2D] = coordinates.compactMap {
    guard let latitude = $0.lat, let longitude = $0.lng else { return nil }
    return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}

This will convert all Coordinates objects that has both a latitude and a longitude given.

Are you sure the properties can be null in Coordinates, if not the above can be simplified to

let points: [CLLocationCoordinate2D] = coordinates.map {
    CLLocationCoordinate2D(latitude: $0.lat, longitude: $0.lng)
}



回答2:


based on Joakim's answer, I figured out the complete solution for this:

So what do you do to pass some coordinates data from screen A (say you have a tableView) to screen B (you have the map).

So in screen A (viewControllerA):

You define the coordinates variable (according to your data model of course, should be an array usually)

var coordinates = [Coordinates]()

Then in your UITableViewDelegate extension, you append your coordinates into your coordinates array. Then use prepare segue to pass it to your destination viewController.

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == Constants.segueTripListToTripMap {
            if let destinationVC = segue.destination as? TripMapViewController,
                let indexPath = sender as? IndexPath {
                
                
                for i in 0...(tripList[indexPath.section][indexPath.row].coordinates.count-1) {
                    self.coordinates.append(tripList[indexPath.section][indexPath.row].coordinates[i])
                }
                
       
                
                destinationVC.coordinatePoints = coordinates
                coordinates = []
                
                 
            }
        } else {
            if let destinationVC = segue.destination as? PolicyOverviewViewController,
                let indexPath = sender as? IndexPath {
                // do something
            }
        }

    }

In your screen B (where you map is). You define the variable to catch the data pass from screen A, which is coordinatePoints

var coordinatePoints: [Coordinates]?

then in your draw route function (or whatever name you call it). You convert this coordinatePoints into CLLocationCoordinate2D type like below:

    func drawRoutes() {

        var points:[CLLocationCoordinate2D] = coordinatePoints?.compactMap {
            guard let latitude = $0.lat, let longitude = $0.lng else { return nil }
            return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
            } ?? [CLLocationCoordinate2DMake(52.3676, 4.9041)]
                

        let polygon = MKPolyline(coordinates: &points, count: points.count)

        self.mapView.addOverlay(polygon)
        self.mapView.setVisibleMapRect(polygon.boundingMapRect, animated: true)

        var startPoint = points[0]
        
        for i in 1...(points.count-1) {
            
            guard let request = createRequest(c1:startPoint, c2:points[i]) else { return }
            let directions = MKDirections(request: request)

            directions.calculate { [unowned self] (response, error) in
                guard let response = response else { return }
                let routes = response.routes
                let bestDest = routes[0]
                startPoint = points[i]
            }
        }
    }

there you go, when you click the table cell in screen A, it should pass the right coordinates into screen B and plot it on the map.



来源:https://stackoverflow.com/questions/64622152/pass-array-of-map-coordinates-to-a-draw-a-route-on-map-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!