使用Firebase数据并在iOS上创建Google Maps API标记

问题描述 投票:-1回答:1

我正在尝试制作iOS应用,使用Google Maps API和Firebase。它应该在地图上显示许多标记。

地图部分运作良好。但是,我想从Firebase实时数据库中检索位置数据(纬度,经度),然后放入marker.position。 (那么它应该创建一个标记,对吧?)

它打了很多天,我仍然无法显示标记(但我可以从Firebase数据库打印正确的数据)。

    ref = Database.database().reference()

    ref.child("locations").observeSingleEvent(of: .value) { (snapshot) in
        if snapshot.exists() {
            if let location = snapshot.value as? [String:Any] {
                for eachLocation in location {
                    print("Location: \(eachLocation)")

                    if let locationCoordinate = eachLocation.value as? [String: Any] {
                        if let lavLatitude = locationCoordinate["latitude"] as? Double {
                            if let lavLongitude = locationCoordinate["longitude"] as? Double {

                                print(lavLatitude)
                                print(lavLongitude)

                                let marker = GMSMarker()
                                marker.position = CLLocationCoordinate2DMake(lavLatitude, lavLongitude)
                                marker.map = mapView
                            }
                        }
                    }
                }
            }
        }
    }

我的代码参考来自Github:appsmall/Map-Demo

当我复制并替换我的GoogleService-Info.plist时,这真的很奇怪。我的应用可以显示Firebase数据库中的标记!

我想知道我的数据库中有什么问题:Screenshot

当然,我在这里搜索并尝试了许多不同的解决方案,但仍然无法正常工作,如果您需要更多信息,请告诉我,谢谢!

ios swift firebase google-maps google-maps-markers
1个回答
1
投票

尝试使用Annotation而不是GMSMarker():

替换它

let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(lavLatitude, lavLongitude)
marker.map = mapView

有了这个

let position = CLLocationCoordinate2D(latitude: lavLatitude, longitude: lavLongitude)
let marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = mapView
© www.soinside.com 2019 - 2024. All rights reserved.