Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps iOS SDK heatmap not rendering

I am following the heat map code here and here for the Google Maps iOS SDK, but the heat map is not rendering on my map. The Google Maps API has been added through Cocoapods, and the Google Maps iOS Utils framework has been added manually with this bridging header:

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

Here is the code for adding the heat map:

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
}

As you can see, it is very simple. I know for a fact that it can find the data because when I print list.count, I get 5, which is indeed the number of entries I have in my police_stations.json. This is what that JSON file looks like:

[
 { "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 }
 ]

In spite of the fact that all of this code is so simple, the heat map does not render, and no errors are thrown, nor are there any warnings.

Why is this not working, and how can I make it work?

like image 255
IHaveAQuestion Avatar asked Jan 25 '18 05:01

IHaveAQuestion


People also ask

Is Google Maps iOS SDK free?

The Maps SDK for iOS uses a pay-as-you-go pricing model.


1 Answers

Try setting heatmapLayer.map = map after you set the weightedData.

I suppose it just doesn't get updated automatically when you add data.

like image 128
Daniel Avatar answered Sep 22 '22 23:09

Daniel