Google 地图 iOS SDK 热图未呈现

Google Maps iOS SDK heatmap not rendering

我正在关注 Google 地图 iOS SDK 的热图代码 here and here,但热图未在我的地图上呈现。 Google 地图 API 已通过 Cocoapods 添加,Google 地图 iOS Utils 框架已通过此桥接 header:[=18= 手动添加]

#import "GMUMarkerClustering.h"
#import "GMUGeometryRenderer.h"
#import "GMUHeatmapTileLayer.h"
#import "GQTPointQuadTree.h"

添加热图的代码如下:

func addHeatmap()  {
    heatmapLayer = GMUHeatmapTileLayer()
    heatmapLayer.map = map
    var list = [GMUWeightedLatLng]()
    do {
        if let path = Bundle.main.url(forResource: "police_stations", withExtension: "json") {
            let data = try Data(contentsOf: path)
            let json = try JSONSerialization.jsonObject(with: data, options: [])
            if let object = json as? [[String: Any]] {
                for item in object {
                    let lat = item["lat"]
                    let lng = item["lng"]
                    let coords = GMUWeightedLatLng(coordinate: CLLocationCoordinate2DMake(lat as! CLLocationDegrees, lng as! CLLocationDegrees), intensity: 1)
                    list.append(coords)
                }
            } else {
                print("Could not read the JSON.")
            }
        }
    } catch {
        print(error.localizedDescription)
    }
    // Add the latlngs to the heatmap layer.
    heatmapLayer.weightedData = list
}

如您所见,非常简单。我知道它可以找到数据,因为当我打印 list.count 时,我得到 5,这确实是我 police_stations.json 中的条目数。这是 JSON 文件的样子:

[
 { "lat" : 40.71916023, "lng" : -74.0001297 },
 { "lat" : 40.73737243, "lng" : -73.98742676 },
 { "lat" : 40.73347023, "lng" : -74.00218964 },
 { "lat" : 40.71083299, "lng" : -74.00424957 },
 { "lat" : 40.79249897, "lng" : -73.96648407 }
 ]

尽管所有这些代码都非常简单,但热图并没有呈现,也没有抛出任何错误,也没有任何警告。

为什么这不起作用,我怎样才能让它起作用?

尝试设置 heatmapLayer.map = map 您设置 weightedData.

之后

我想它只是在您添加数据时不会自动更新。