[英]Not able to convert from Array data to CLLocationCoordinate2D in swift to draw a Polyline in mapview
I have array ( coordinatesArray
) of data which has double values like following. 我有数据(
coordinatesArray
)的数组,它具有如下所示的双精度值。
(
"-61.4149691",
"42.9572076"
)
(
"-61.4126004",
"42.9572026"
)
(
"-61.476674304",
"42.9572032"
)
This is mapData 这是mapData
(
"-61.4149691",
"42.9572076"
)
So, I took a loop for converting each index data as latitude and longitude by converting as double. 因此,我采用了一个循环,通过将两倍转换为索引,将每个索引数据转换为纬度和经度。
if let coordinatesArray = geoMetrys.value(forKey: "coordinates") as? NSArray {
for mapData in coordinatesArray {
print(mapData)
let latitude = (mapData[0] as NSString).doubleValue
let longitude = (mapData[1] as NSString).doubleValue
var coordinatesToAppend: CLLocationCoordinate2D? = [latitude, longitude]
var polyline = MKPolyline(coordinates: &coordinatesToAppend!, count: coordinatesArray.count)
mapView.add(polyline)
But, its showing error while compiling time. 但是,它在编译时显示错误。
`Type 'Any' has no subscript members` for following lines
let latitude = (mapData[0] as NSString).doubleValue
let longitude = (mapData[1] as NSString).doubleValue
I have an array in that I have data of value as latitude and longitude. 我有一个数组,其中有经度和纬度值。 So, I am trying to draw a polyline in mapview with these values.
因此,我正在尝试使用这些值在mapview中绘制一条折线。
Can anyone suggest me where I am failing to achieve this? 谁能建议我在哪里我做不到?
In your code, the line 在您的代码中,该行
if let coordinatesArray = geoMetrys.value(forKey: "coordinates") as? NSArray
returns an array of type Any
(ie [Any]
). 返回类型为
Any
的数组(即[Any]
)。 After that, when you use the for loop 之后,当您使用for循环时
for mapData in coordinatesArray
you are essentially looping over an array of Any
where, in every iteration, an item (of type Any
) is assigned to mapData
. 您实际上是遍历
Any
数组,在每次迭代中,都将一个( Any
类型的)项分配给mapData
。 Now if this mapData
is an array itself, you have to explicitly specify it as 现在,如果此
mapData
本身是数组,则必须将其显式指定为
if let dataArray = mapData as? NSArray {
let latitude = (dataArray[0] as NSString).doubleValue
let longitude = (dataArray[1] as NSString).doubleValue
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.