Ios 谷歌地图绘制多纬度经度之间的多段线
我有一个经纬度数组,我想用这些数据画多段线 数组数据如下:Ios 谷歌地图绘制多纬度经度之间的多段线,ios,google-maps,swift5,polyline,Ios,Google Maps,Swift5,Polyline,我有一个经纬度数组,我想用这些数据画多段线 数组数据如下: [ { address = "Gota, Godrej Garden City Road, Ahmedabad, India, 382481 ", city = Ahmedabad, latitude = "23.10104251103548", longitude = "72.54941169820619", street = "Godrej G
[
{
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,因为我已经拥有所有这些经纬度数据。。。
即使为所有一百条记录调用DirectionAPI也不是一个可行的解决方案
我上面的代码没有给我任何错误。它正在地图上绘制两个管脚,这对我来说似乎是正确的[从数组的第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
}
func drawPolyline(){
let位置:CLLocationCoordinate2D=CLLocationCoordinate2D(纬度:23.10104251103548,经度:72.54941169820619)
让摄像头=GMSCameraPosition.摄像头(带目标:位置,缩放:18)
self.mapContentView.mapView.animate(到:摄影机)
设arrLat=[23.10104251103548,23.09644251103548,23.08644251103548,23.07644251103548,23.06644251103548]
假设arrLng=[72.54941169820619,72.5480169820619,72.54701169820619,72.54601169820619,72.54501169820619]
let path=GMSMutablePath()
因为我在0
我认为你应该首先在你的应用程序中尝试一些lat&lng示例。如果它起作用,那么问题来自你的解析数据。确保你的mapData。纬度和mapData。经度是数字和>0@TraiNguyen是的。是的,你是对的。我的代码是完美的。问题是经纬度数据。谢谢你的回答。