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

问题描述 投票:1回答:1

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

数组数据如下,

[
     {
        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 Direction API,但是我不需要调用该api,因为我已经拥有所有这些纬度和经度数据...甚至为所有一百条记录调用Direction api也不可行。

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

enter image description here

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

ios google-maps swift5 polyline
1个回答
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

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

© www.soinside.com 2019 - 2024. All rights reserved.