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

setStyleUrl with callback #8262

Closed
Closed

Description

@tobrun

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

Activity

  1. added
    Android
    on Mar 2, 2017
  2. 1ec5 commented on Mar 2, 2017

    @1ec5
    Contributor

    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).

  3. self-assigned this
    on Mar 7, 2017
  4. tobrun commented on Mar 10, 2017

    @tobrun
    MemberAuthor

    landed with #8291

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Android

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions