Examples
Dynamic styling (32)

Use GeoJSON data to set extrusion height

package com.mapbox.mapboxandroiddemo.examples.extrusions;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.style.layers.FillExtrusionLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import java.io.IOException;
import java.io.InputStream;
import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillExtrusionColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillExtrusionHeight;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillExtrusionOpacity;
/**
* Use data-driven styling and GeoJSON data to set extrusions' heights
*/
public class MarathonExtrusionActivity extends AppCompatActivity implements OnMapReadyCallback {
private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(this, getString(R.string.access_token));
// This contains the MapView in XML and needs to be called after the access token is configured.
setContentView(R.layout.activity_marathon_extrusion);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.SATELLITE, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
// Add the marathon route source to the map
// Create a GeoJsonSource and use the Mapbox Datasets API to retrieve the GeoJSON data
// More info about the Datasets API at https://www.mapbox.com/api-documentation/#retrieve-a-dataset
GeoJsonSource courseRouteGeoJson = new GeoJsonSource(
"coursedata", loadJsonFromAsset("marathon_route.geojson"));
style.addSource(courseRouteGeoJson);
addExtrusionsLayerToMap(style);
}
});
}
private void addExtrusionsLayerToMap(@NonNull Style loadedMapStyle) {
// Add FillExtrusion layer to map using GeoJSON data
loadedMapStyle.addLayer(new FillExtrusionLayer("course", "coursedata").withProperties(
fillExtrusionColor(Color.YELLOW),
fillExtrusionOpacity(0.7f),
fillExtrusionHeight(get("e"))));
}
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
private String loadJsonFromAsset(String filename) {
// Using this method to load in GeoJSON files from the assets folder.
try {
InputStream is = getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
}