【问题标题】:Using Firebase data and create Google Maps API markers on iOS在 iOS 上使用 Firebase 数据并创建 Google Maps API 标记
【发布时间】:2019-08-11 13:55:18
【问题描述】:

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

地图部分运行良好。但是,我想从 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

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

【问题讨论】:

  • 你的标记有iconView (marker.iconView = someView)吗?
  • 没有marker.iconView,只是普通的谷歌地图标记
  • 尝试用这个marker.position = CLLocationCoordinate2D(latitude: lavLatitude, longitude: lavLongitude)替换你的标记位置
  • 不,我试过了。
  • 我认为问题出在 firebase 数据库......

标签: ios swift firebase google-maps google-maps-markers


【解决方案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

【讨论】:

  • 注解()?它显示红色警告标志:使用未解析的标识符“注释”
  • 对不起,它仍然在 "mapView.addAnnotation(placeAnnotation)" => "Value of type GMSMapView has no member 'addAnnotation';你的意思是 'addInteraction' 吗?"
  • @Stijnk008 他没有使用MapKit,他使用的是GoogleMaps
猜你喜欢
  • 2017-07-30
  • 2018-12-11
  • 2017-03-18
  • 1970-01-01
  • 2019-07-13
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多