Google 地图在多个纬度经度之间绘制折线

Google Map Draw Polyline between multiple latitude longitude

我有一个纬度和经度数组,我想使用这些数据绘制折线。

数组数据如下,

[
     {
        address = "Gota, Godrej Garden City Road, Ahmedabad, India, 382481 ",
        city = Ahmedabad,
        latitude = "23.10104251103548",
        longitude = "72.54941169820619",
        street = "Godrej Garden City Road",
     },
     {
        address = "Aaryan Eureka Opposite Shayona Shikhar, Vandemataram Rd, Gota, Ahmedabad, Gujarat 382481",
        city = Ahmedabad,
        latitude = "23.09644251103548",
        longitude = "72.54801169820619",
        street = "Vandemataram Rd",
     },
     ....// Hundreds of records
]

到目前为止我试过的代码,

let path = GMSMutablePath()
    for mapData in arrMapData {
        path.add(CLLocationCoordinate2D(latitude: mapData.latitude ?? 0.0, longitude: mapData.longitude ?? 0.0))
    }
    let polyline = GMSPolyline(path: path)
    polyline.strokeWidth = 3.0
    polyline.strokeColor = .black
    polyline.map = mapView // Google MapView

    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: arrMapData[0].latitude ?? 0.0, longitude: arrMapData[0].longitude ?? 0.0)
    marker.icon = UIImage(named: AppImages.Pin_red)
    marker.title = sourceTitle
    marker.snippet = sourceSnippet
    marker.map = mapView

    let marker1 = GMSMarker()
    marker1.position = destination
    marker1.icon = UIImage(named: AppImages.Map_pin_orange)
    marker1.title = destinationTitle
    marker1.snippet = destinationSnippet
    marker1.map = mapView

我知道 Google 方向 API,但我不需要调用它 api 因为我已经有了所有这些纬度和经度数据... 甚至所有一百条记录都调用方向api也不是可行的方案。

我上面的代码没有给我任何类型的错误。它在地图上绘制了两个图钉,这对我来说似乎是正确的[从数组中的第 0 个索引对象和数组中的最后一个索引对象添加的源目的地详细信息]。

但是地图上没有添加折线。我正在寻找一些解决方案。 谢谢!

根据您的代码,我将数组 lat 和 lng 修改为以下函数

func drawPolyline(){
        let location: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 23.10104251103548, longitude: 72.54941169820619)
        let camera = GMSCameraPosition.camera(withTarget: location, zoom: 18)
        self.mapContentView.mapView.animate(to: camera)

        let arrLat = [23.10104251103548, 23.09644251103548, 23.08644251103548, 23.07644251103548, 23.06644251103548]
        let arrLng = [72.54941169820619, 72.54801169820619, 72.54701169820619, 72.54601169820619, 72.54501169820619]
        let path = GMSMutablePath()
        for i in 0..<arrLat.count {
            path.add(CLLocationCoordinate2D(latitude: arrLat[i], longitude: arrLng[i]))
        }
        let polyline = GMSPolyline(path: path)
        polyline.strokeWidth = 3.0
        polyline.strokeColor = .black
        polyline.map = self.mapContentView.mapView

        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: 23.10104251103548, longitude: 72.54941169820619)
        marker.icon = UIImage(named: "ic_marker")
        marker.title = "A"
        marker.snippet = "ABC"
        marker.map = self.mapContentView.mapView

        let marker1 = GMSMarker()
        marker1.position = CLLocationCoordinate2D(latitude: 23.06644251103548, longitude: 72.54501169820619)
        marker1.icon = UIImage(named: "ic_marker")
        marker1.title = "B"
        marker1.snippet = "EFG"
        marker1.map = self.mapContentView.mapView
    }

结果HERE

我认为您应该先在您的应用中尝试使用一些经纬度示例。如果有效,那么问题出在您的解析数据上。