Google Maps iOS SDK热图未呈现

IHa*_*ion 5 google-maps heatmap ios google-maps-sdk-ios swift

我在此处此处遵循Google Maps iOS SDK 的热图代码,但是热图未在我的地图上呈现。已通过Cocoapods添加了Google Maps API,并使用此桥接标头手动添加了Google Maps iOS Utils框架:

#import "GMUMarkerClustering.h"
#import "GMUGeometryRenderer.h"
#import "GMUHeatmapTileLayer.h"
#import "GQTPointQuadTree.h"
Run Code Online (Sandbox Code Playgroud)

这是添加热图的代码:

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
}
Run Code Online (Sandbox Code Playgroud)

如您所见,这非常简单。我知道它可以找到数据,因为我打印时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 }
 ]
Run Code Online (Sandbox Code Playgroud)

尽管所有这些代码是如此简单,但热图不会呈现,也不会引发任何错误,也没有任何警告。

为什么这不起作用,如何使它起作用?

Dan*_*iel 6

尝试设置heatmapLayer.map = map 之后您设置的weightedData

我想添加数据时它不会自动更新。