This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
setStyleUrl with callback #8262
Copy link
Copy link
Closed
Labels
AndroidMapbox Maps SDK for Android
Description
Style loading happens asynchronous and users are required to wait until the style has finished loading before altering any of the style properties. Capturing from #8144 that this isn't always obvious. For example they can write code as:
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.setStyleUrl(Style.SATELLITE);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull final MapboxMap map) {
mapboxMap = map;
mapboxMap.setStyleUrl(Style.MAPBOX_STREETS);
try {
mapboxMap.addSource(new GeoJsonSource("bus_stop",
new URL("https://raw.githubusercontent.com/cheeaun/busrouter-sg/master/data/2/bus-stops.geojson")));
} catch (MalformedURLException malformedUrlException) {
Timber.e("That's not an url... ", malformedUrlException);
}
layer = new CircleLayer("stops_layer", "bus_stop");
layer.setProperties(
circleColor(Color.parseColor("#FF9800")),
circleRadius(1.0f)
);
mapboxMap.addLayer(layer);
}
}Executing this code would result in adding the GeoJsonSource and the CircleLayer to Style.SATELLITE instead of Style.MAPBOX_STREETS. Having setStyleUrl(String, Callback)would make it more obvious from API perspective + will remove the burden of writing boiler plate map change event code as:
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull final MapboxMap map) {
mapboxMap = map;
mapView.addOnMapChangedListener(new MapView.OnMapChangedListener() {
@Override
public void onMapChanged(@MapView.MapChange int change) {
if (change == MapView.DID_FINISH_LOADING_STYLE) {
try {
mapboxMap.addSource(new GeoJsonSource("bus_stop",
new URL("https://raw.githubusercontent.com/cheeaun/busrouter-sg/master/data/2/bus-stops.geojson")));
} catch (MalformedURLException malformedUrlException) {
Timber.e("That's not an url... ", malformedUrlException);
}
layer = new CircleLayer("stops_layer", "bus_stop");
layer.setProperties(
circleColor(Color.parseColor("#FF9800")),
circleRadius(1.0f)
);
mapboxMap.addLayer(layer);
}
}
});
mapboxMap.setStyleUrl(Style.SATELLITE);
}
});cc @mapbox/android
Reacted by Jonathan Santos
Reacted by Cam Mace and Antonio Zugaldia
Activity
1ec5 commentedon Mar 2, 2017
The iOS/macOS equivalent is being tracked in #6386 (comment) (in conjunction with making it so that the developer sets a style rather than a style URL).
tobrun commentedon Mar 10, 2017
landed with #8291