Search code examples
androidmapbox-android

How to create Mapbox hotspots and heatmap from lat-long list in Android?


I have a database table containing lat-long data that I need to show as hotspots or as a heatmap on a Mapbox map.

I've checked out the official examples for hotspots and heatmaps, but they are both loading their data from a URL (in geojson format).

What changes do I need to make to their example code (Java and not deprecated), so the hotspot/heatmap data is loaded from my array of LatLng objects instead?


Solution

  • List<LatLng> latLngListFromDatabase = new ArrayList<>();
        List<Feature> featureList = new ArrayList<>();
    
        for (LatLng latLngObject : latLngListFromDatabase) {
          featureList.add(Feature.fromGeometry(Point.fromLngLat(latLngObject.getLongitude(), latLngObject.getLatitude())));
        }
    
        loadedMapStyle.addSource(new GeoJsonSource("source-id", FeatureCollection.fromFeatures(featureList)));