在Swift中声明CLLocationCoordinate2DMake数据类型变量

时间:2020-10-31 10:38:42

标签: ios swift dictionary mapkit segue

我有两个屏幕,从屏幕A开始,我将一些GPS坐标传递到屏幕B。我已经使用一些硬编码数据成功在屏幕B中绘制了路线。

像这样:

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

但是我不知道如何在屏幕B中定义一个变量,以使用“准备缝制”来接收从屏幕A传递的值。

 var coodinatePoints: [CLLocationCoordinate2DMake]?

这实际上不起作用,它表示未声明的类型CLLocationCoordinate2DMake ...

我可以问一下如何在屏幕B中创建一个变量来替换我的硬编码坐标,以及如何获取从屏幕A中传递的坐标。

坐标数据类型如下:

 "coordinates": [
          {
            "lat": 0,
            "lng": 0,
            "time": "2020-10-31T10:37:01.757Z"
          }
        ]

非常感谢!

2 个答案:

答案 0 :(得分:1)

类型为CLLocationCoordinate2D

核心位置可以追溯到Swift之前。在Objective C中,结构无法像在Swift中那样具有初始化程序,因此使用了构造函数。

CLLocationCoordinate2DMake是一个构造CLLocationCoordinate2D的函数。

您可以将您的财产声明为

var coodinatePoints: [CLLocationCoordinate2D]?

答案 1 :(得分:-1)

我已经找到了完整的解决方案:(也请检查我在此处的堆栈溢出中询问的另一个问题:pass array of map coordinates to a draw a route on map in swift

那么您要怎么做才能将一些坐标数据从屏幕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中并将其绘制在地图上。

enter image description here

相关问题