【问题标题】:How do I get the latitude and longitude out of my array structure and create a marker?如何从数组结构中获取纬度和经度并创建标记?
【发布时间】:2019-02-13 16:13:34
【问题描述】:

我是 swift 新手,目前正试图让我的应用在我的 Google 地图视图中显示标记。纬度和经度保存在数组结构中。出于某种原因,我无法从我的数组中获取数据。 所以我的主要问题是它为什么不工作以及它将如何工作?

此外,我想知道将结构元素设置为 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)) 
})

【问题讨论】:

  • 您能具体说明您的问题吗? “由于某种原因,我无法从数组中取出数据”是什么意思? pickers 来自哪里?我真的很想知道在实际状态下谁会赞成这个问题......
  • @Teetz 如你所愿更新。 “由于某种原因,我无法从数组中取出数据”,我的意思是我想知道为什么我不能通过像 'picker.lat' 这样的方式来获取我的变量,因为它实际上是 pickerStruct 的成员
  • @LeoDabus 哦,谢谢。它成功了,那真是愚蠢^^

标签: arrays swift google-maps


【解决方案1】:

问题在于,当您应该访问元素属性时,您却试图访问数组 lat 属性。

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

【讨论】:

    【解决方案2】:

    就像在 cmets 中提到的 Leo Dabus,你的 showPickers 函数应该是这样的:(你使用的是数组选择器而不是一个元素选择器结构)

    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: pickerStruct.lat, longitude: pickerStruct.long)
                marker.map = mapView
    
            }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-07-14
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 2011-09-16
      相关资源
      最近更新 更多