【发布时间】:2018-01-25 05:33:32
【问题描述】:
我正在关注 Google Maps iOS SDK 的热图代码 here 和 here,但热图未在我的地图上呈现。 Google Maps API 已通过 Cocoapods 添加,Google Maps iOS Utils 框架已通过此桥接头手动添加:
#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 }
]
尽管所有这些代码都很简单,但热图并没有呈现,也没有抛出任何错误,也没有任何警告。
为什么这不起作用,我怎样才能使它起作用?
【问题讨论】:
标签: ios swift google-maps heatmap google-maps-sdk-ios