arrays - 如何从数组结构中获取纬度和经度并创建标记?
问题描述
我是 swift 新手,目前正试图让我的应用程序在我的谷歌地图视图中显示标记。纬度和经度保存在数组结构中。出于某种原因,我无法从我的数组中获取数据。所以我的主要问题是它为什么不工作以及它将如何工作?
除此之外,我想知道将结构元素设置为 CLLocation 类型是否可行?
struct pickerStruct
{
var lat: Double
var long: Double
}
func showPickers() {
//For every pickerStruct in pickers create a picker
//The marker should have the in pickers saved latitude and longitude
for pickerStruct in pickers {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: pickers.lat, longitude: pickers.long)
marker.map = mapView
}
}
我得到的错误说:“类型'[pickerStruct]'的值没有成员'lat'”
采摘者来自哪里:
Pickers 是一个从我的 Firebase 数据库中获取数据的数组。
ref = Database.database().reference()
ref.child("locations").observe(.childAdded, with: { snapshot in
let newLat = snapshot.value as? NSDictionary
let lat:Double = (newLat?["lat"] as? Double)!
let newLong = snapshot.value as? NSDictionary
let long:Double = (newLong?["long"] as? Double)!
self.pickers.append(pickerStruct(lat: lat, long: long))
})
解决方案
那里的问题是,当您应该访问元素属性时,您正在尝试访问数组 lat 属性。
for picker in pickers {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: picker.lat, longitude: picker.long)
marker.map = mapView
}
推荐阅读
- python - 如何将 4d numpy 图像数组转换为 3d?
- cordova - 在为 android 构建离子应用程序时无法读取未定义的属性“地图”
- laravel - 无法连接到服务器:端口 5432 和 54320 的连接被拒绝取决于我在做什么
- javascript - 如何在后端创建验证下拉列表的功能
- python - 如何根据链接到请求的 slug 的类别显示查询集?
- laravel - 如果特定字段等于某物,如何将部分添加到表单中?
- android - 如何始终在底部工作表对话框片段中将按钮与屏幕底部对齐
- javascript - 具有倍数级别的过滤器数组
- logging - SPaudit-w3wp 吃掉我的记忆-sharepoint 2019
- java - 如何通过在片段内单击按钮多次膨胀视图/容器?