首页 > 解决方案 > 如何从数组结构中获取纬度和经度并创建标记?

问题描述

我是 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)) 
})

标签: arraysswiftgoogle-maps

解决方案


那里的问题是,当您应该访问元素属性时,您正在尝试访问数组 lat 属性。

for picker in pickers {
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: picker.lat, longitude: picker.long)
    marker.map = mapView
}

推荐阅读