3

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?

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
  • 1
    You can try writing your LatLng data in formatted geojson file (e.g. "myLatLng.geojson"), and put it in the assets directory. And then replacing the remote source URL with the local one: "file:///android_asset/myLatLng.geojson" – Khal91 Dec 11 '20 at 19:51
  • 1
    @Khal91 I was considering something like that as a last resort, but fortunately I now have an answer now that doesn't involve going round the houses. Cheers, anyway. – ban-geoengineering Dec 13 '20 at 16:16

2 Answers2

3
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)));
langsmith
  • 2,529
  • 1
  • 11
  • 13
2

To convert your your LatLng objects to Feature use:

var feature = Feature.fromGeometry(Point.fromLngLat(longitude, latitude))

and add them to a featureList. Then you can create an instance of FeatureCollection with:

public static FeatureCollection fromFeatures(@NonNull List<Feature> features)

Finally, there is a constructor in GeoJsonSource in which you can use a FeatureCollection instead of URI:

public GeoJsonSource(String id, FeatureCollection features, GeoJsonOptions options)
Sina
  • 2,683
  • 1
  • 13
  • 25