Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

setStyleUrl with callback #8262

Closed
tobrun opened this issue Mar 2, 2017 · 2 comments
Closed

setStyleUrl with callback #8262

tobrun opened this issue Mar 2, 2017 · 2 comments
Assignees
Labels
Android Mapbox Maps SDK for Android

Comments

@tobrun
Copy link
Member

tobrun commented Mar 2, 2017

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

@tobrun tobrun added the Android Mapbox Maps SDK for Android label Mar 2, 2017
@1ec5
Copy link
Contributor

1ec5 commented 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
Copy link
Member Author

tobrun commented Mar 10, 2017

landed with #8291

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Android Mapbox Maps SDK for Android
Projects
None yet
Development

No branches or pull requests

2 participants