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.

[Android] Expose styling functionality for annotations #6172

Closed

Description

So I've managed to figure out how to display a dotted polyline with the new styling API introduced in the 4.2 beta SDK:

dots

This isn't achievable without hacks though, there's currently two issues with my approach:

  • There's no direct way of accessing the Polyline's layer to apply styling changes. Short of re-implementing line_annotation_impl.cpp myself in Java along with all the tilemap and source stuff, this makes customising the Polyline very difficult. My workaround here is to access the layer once the Polyline's been added to the map, which leads to the second issue...
  • There's no public way of getting the names of the Polyline layers - from investigating the source code these layers are prefixed with com.mapbox.annotations.shape. and followed by the Polyline object's ID, but obviously this could change at any time, so it's not particularly reliable.

My current WIP solution is the following code:

        final Polyline polyline = myMap.addPolyline(
            new PolylineOptions()
                .add(points)
                .color(context.getResources().getColor(R.color.lineColor))
                .width(10)
        );

        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                final LineLayer layer =
                    myMap.getLayerAs("com.mapbox.annotations.shape." + polyline.getId());

                if (layer != null) {
                    layer.setProperties(
                        PropertyFactory.lineDasharray(new Float[] {
                            0.01f, 2f
                        }),
                        PropertyFactory.lineCap("round"),
                        PropertyFactory.lineJoin("round")
                    );
                }
            }
        });

As you can see there's a lot wrong with this, but I don't see any other way of doing this as it stands with the current SDK.

Is it possible to add additional support in the SDK for either A. querying an annotation's layer in order to apply styling information, or B. provide a utility method to apply styling information and have the native map do all of the heavy lifting with the layers?

Activity

  1. 1ec5 commented on Aug 26, 2016

    @1ec5
    Contributor

    Although the layer API represents shape annotations as line and fill layers, shape annotations are a bit special. In particular, shape annotations persist even after changing or reloading the style, whereas layers do not.

    What if PolylineOptions had a layer property that took a LineLayer object? The layer itself would be ignored, but all its style properties would be applied to the actual annotation in AnnotationManager so that they'd persist across Styles as the annotation does.

  2. 1ec5 commented on Aug 26, 2016

    @1ec5
    Contributor

    That sounds reasonable on the Android side, where there's a single entry point for adding style properties.

    We'd need to plumb the options down to mbgl::LineAnnotationImpl::updateStyle(). The individual properties live on the layer subclasses themselves, so either we'd have to factor out an abstract interface for them or pass in a "prototype layer" that the annotation would draw style properties from. I think the latter approach would actually be pretty elegant.

  3. added
    Android
    Core
    annotations
    on Aug 26, 2016
  4. jfirebaugh commented on Aug 26, 2016

    @jfirebaugh
    Contributor

    If you want to use more styling features than are provided by annotations, I recommend you use the runtime styling APIs directly, and add a GeoJSON source and line layer.

  5. 1ec5 commented on Aug 26, 2016

    @1ec5
    Contributor

    Agreed, a GeoJSON source combined with a line layer will likely address your particular use case.

    Nonetheless, I think it would be desirable to move the annotation and runtime styling APIs closer together, to the point that the former would be a mere special case of the latter. Particularly when it comes to points, the current situation requires developers to choose between the two depending on whether style customization or mutability (or compatibility with the Google Maps SDK) is more important. On the iOS side, we want similar functionality for parity with MapKit’s -[MKOverlayPathRenderer lineDashPattern] and similar options: #1734.

  6. 1ec5 commented on Aug 26, 2016

    @1ec5
    Contributor

    It should be possible for what you’ve described. These classes may be useful to you:

  7. jfirebaugh commented on Sep 12, 2016

    @jfirebaugh
    Contributor

    Closing; I think we've covered the bases here:

    • Outlined how to accomplish this with runtime styling API
    • Linked to existing tickets about expanding the annotation-based style properties.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    AndroidCoreannotationsfeatureruntime styling

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions