【问题标题】:pass array of map coordinates to a draw a route on map in swift将地图坐标数组传递给快速在地图上绘制路线
【发布时间】:2021-02-13 17:29:43
【问题描述】:

嗨,我有一个这样的坐标数据数组,它是从 API 调用中检索的,我使用 for 循环将数据附加到这样的数组中:

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

    }

}

它有一个错误...我不知道如何将此数组传递给我在另一个屏幕中声明的变量,数据类型似乎不匹配。

原始数据类型如下所示:

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]

]

如何将这些数据传递到我的绘制路线函数的点变量中。它目前使用一些虚拟数据进行硬编码。

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

非常感谢!

【问题讨论】:

    标签: ios swift dictionary mapkit


    【解决方案1】:

    您可以使用map 转换您的坐标

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

    这将转换所有同时具有纬度和经度的坐标对象。

    你确定坐标中的属性可以为空吗,如果不是,上面可以简化为

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

    【讨论】:

      【解决方案2】:

      根据 Joakim 的回答,我找到了完整的解决方案:

      那么如何将一些坐标数据从屏幕 A(假设您有一个 tableView)传递到屏幕 B(您有地图)。

      所以在屏幕A(viewControllerA)中:

      你定义坐标变量(当然根据你的数据模型,通常应该是一个数组)

      var coordinates = [Coordinates]()
      
      

      然后在 UITableViewDelegate 扩展中,将坐标附加到坐标数组中。然后使用 prepare segue 将其传递给您的目标 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
                  }
              }
      
          }
      

      在您的屏幕 B 中(您映射的位置)。您定义变量以捕获来自屏幕 A 的数据传递,即coordinatePoints

      var coordinatePoints: [Coordinates]?
      

      然后在您的绘制路线函数中(或您所称的任何名称)。您将这个 coordinatePoints 转换为 CLLocationCoordinate2D 类型,如下所示:

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

      你去吧,当你点击屏幕 A 中的表格单元格时,它应该将正确的坐标传递到屏幕 B 并将其绘制在地图上。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-23
        • 1970-01-01
        • 1970-01-01
        • 2014-06-01
        • 2012-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多