This repository was archived by the owner on Oct 7, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Oct 7, 2024. It is now read-only.
Mapbox - Heatmap action item #560
Copy link
Copy link
Closed
Description
Here is the snippet which I used to get heatmap points when the user clicks on the map.
Feature list (featureList0) always comes as "empty"
private void addClusteredGeoJsonSource(final MapboxMap mapboxMap) {
// Add a new source from our GeoJSON data and set the 'cluster' option to true.
try {
mapboxMap.addSource(
// Point to GeoJSON data. This example visualizes all M1.0+ earthquakes from
// 12/22/15 to 1/21/16 as logged by USGS' Earthquake hazards program.
new GeoJsonSource("test",
new URL("https://www.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson"),
new GeoJsonOptions()
.withCluster(true)
.withClusterMaxZoom(15) // Max zoom to cluster points on
.withClusterRadius(20) // Use small cluster radius for the heatmap look
)
);
} catch (MalformedURLException malformedUrlException) {
Log.e("heatmapActivity", "Check the URL " + malformedUrlException.getMessage());
}
// Use the earthquakes source to create four layers:
// three for each cluster category, and one for unclustered points
// Each point range gets a different fill color.
final int[][] layers = new int[][]{
new int[]{150, Color.parseColor("#E55E5E")},
new int[]{20, Color.parseColor("#F9886C")},
new int[]{0, Color.parseColor("#FBB03B")}
};
CircleLayer unclustered = new CircleLayer("points", "test");
unclustered.setProperties(
circleColor(Color.parseColor("#FBB03B")),
circleRadius(20f),
circleBlur(1f));
unclustered.setFilter(
neq("cluster", true)
);
mapboxMap.addLayerBelow(unclustered, "test1");
for (int i = 0; i < layers.length; i++) {
CircleLayer circles = new CircleLayer("cluster-" + i, "test");
circles.setProperties(
circleColor(layers[i][1]),
circleRadius(70f),
circleBlur(1f)
);
circles.setFilter(
i == 0
? gte("point_count", layers[i][0]) :
all(gte("point_count", layers[i][0]), lt("point_count", layers[i - 1][0]))
);
mapboxMap.addLayerBelow(circles, "test1");
}
final int diff = 50;
mapboxMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() {
@Override
public void onMapClick(@NonNull LatLng point) {
PointF pointf = mapboxMap.getProjection().toScreenLocation(point);
RectF rectF = new RectF(pointf.x - diff, pointf.y - diff, pointf.x + diff, pointf.y + diff);
List<Feature> featureList0 = mapboxMap.queryRenderedFeatures(rectF, "points");
int i = 0;
for (com.mapbox.services.commons.geojson.Feature feature : featureList0) {
// Log.d("Feature found with %1$s", feature.toJson());
Log.d("heat_map","Feature:"+ feature.getProperties().toString());
JsonElement media = feature.getProperty("mMedia");
if(media != null) {
Log.d("heat_map","Feature: media:" +media);
} else {
Log.d("heat_map","Feature: media-:null");
}
i++;
}
}
});
}
Metadata
Metadata
Assignees
Labels
No labels
Activity
zugaldia commentedon Dec 1, 2017
cc: @langsmith
langsmith commentedon Dec 16, 2017
@Dino9 , thanks for using Mapbox. Try the code below. I was able to get
featureList0to return a size of 1 if not more in theCreateHotspotsActivitydemo app example.In this example, the returned list size seems to depend on the map camera zoom level and exactly where I tap. I encourage you to try the code below and then play around with the zoom and tap location.
Also seems that you're not using the
iint that you set withint i = 0;and then++in the for loop...langsmith commentedon Dec 16, 2017
@Dino9 , also meant to say that my code changes the list name from
featureList0tolistOfFeatures. Also, you'll notice that I use static strings for the source/layer ids. I find that this helps to keep clear on what's what when doing runtime/data-driven styling. Anyways, here's the entire example's code for you to try. Features do return in your finalforloop 👇langsmith commentedon Dec 16, 2017
I consider this solved. Please re-open if there's still an issue.