mapbox-gl-native icon indicating copy to clipboard operation
mapbox-gl-native copied to clipboard

Expose a generic Android View Marker API

Open tobrun opened this issue 10 years ago • 70 comments

Current Marker API on android is fairly limited because Markers are Gl-drawn components. I'm hearing about requests for animating markers or having the ability to do more typical Android SDK visual things with it (animations, selectors etc.).

Concurrent InfoWindows from #3127 and UserLocationView showcased that we are able to sync Android Views with the underlying MapView. The logic is their but we do not expose a generic system.

API proposal:

For native annotations views in the Android Mapbox SDK. I don't see any reason to stray far from the adapter concept what an Android developer uses on daily basis. Combining this with the existing Mapbox API for adding annotations will look like:

 // combined API
 addMarker();
 addMarkers();
 removeMarker();
 removeMarkers();
 selectMarker();
 // new API
 setMarkerViewAdapter();
 setOnMarkerViewClickListener();
 setMarkerViewItemAnimation();

Example API

Integration of the API will follow the same path as the current Marker API.

  // Add country markers
  List<BaseMarkerOptions> countries = new ArrayList<>();
  countries.add(new CountryMarkerOptions().title("China").abbrevName("ch").flagRes(R.drawable.ic_china).position(new LatLng(31.230416, 121.473701)));
  countries.add(new CountryMarkerOptions().title("United States").abbrevName("us").flagRes(R.drawable.ic_us).position(new LatLng(38.907192, -77.036871)));
  countries.add(new CountryMarkerOptions().title("Brazil").abbrevName("br").flagRes(R.drawable.ic_brazil).position(new LatLng(-15.798200, -47.922363)));
  countries.add(new CountryMarkerOptions().title("Germany").abbrevName("de").flagRes(R.drawable.ic_germany).position(new LatLng(52.520007, 13.404954)));
  mapboxMap.addMarkers(countries);

On top of that it will expose some new APIs to make ViewMarkers possible:

  // Add view marker adapter
  mapboxMap.setMarkerViewAdapter(new CountryAdapter(this));

  // Add a view marker click listener
  mapboxMap.setOnMarkerViewClickListener(new MapboxMap.OnMarkerViewClickListener() {
            @Override
            public void onMarkerClick(@NonNull Marker marker, @NonNull View view) {
                Log.d(MapboxConstants.TAG, "Country clicked " + ((CountryMarker) marker).getAbbrevName());
            }
        });

An example of the MarkerViewAdapter shown above:

    private static class CountryAdapter implements MapboxMap.MarkerViewAdapter<CountryMarker> {

        private LayoutInflater inflater;

        public CountryAdapter(@NonNull Context context) {
            this.inflater = LayoutInflater.from(context);
        }

        @Nullable
        @Override
        public View getView(@NonNull CountryMarker marker, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = inflater.inflate(R.layout.view_custom_marker, parent, false);
                viewHolder.flag = (ImageView) convertView.findViewById(R.id.imageView);
                viewHolder.abbrev = (TextView) convertView.findViewById(R.id.textView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.flag.setImageResource(marker.getFlagRes());
            viewHolder.abbrev.setText(marker.getAbbrevName());
            return convertView;
        }

        private static class ViewHolder {
            ImageView flag;
            TextView abbrev;
        }
    }

Next steps:

  • [x] Basic tracking view on Mapview - @tobrun
  • [x] Research limitations + perf. impacts - @tobrun
  • [x] Test out Android SDK animations - @tobrun
  • [x] Look into different approaches to publicly expose the API - @tobrun
  • [x] Introduce basic View Adapter approach - @tobrun
  • [x] Use a View reuse pattern in adapter (SimplePool) - @tobrun
  • [x] Integrate with selectMarker(Marker m) API - @tobrun
  • [x] Integrate with removeMarker(Marker m) API - @tobrun
  • [x] Hide old GL marker - @tobrun
  • [x] Trigger invalidate on View markers when Map is fully loaded - @tobrun
  • [x] Validate is exposed Android View Marker API matches iOS View Annotation API - @tobrun
  • [x] Profile execution - @tobrun
  • [x] Animation system - @tobrun
  • [x] Select animation API - @tobrun
  • [x] allow adding multiple adapters - @tobrun
  • [x] Flat configurable
  • [x] Offset configurable
  • [x] Double check changes made to basic Marker API - @tobrun
  • [ ] API documentation - @tobrun

@incanus @bleege @zugaldia @cammace @danswick

tobrun avatar Dec 14 '15 01:12 tobrun

Related: https://github.com/mapbox/mapbox-gl-native/issues/3116

zugaldia avatar Dec 14 '15 20:12 zugaldia

Related: #1125

tobrun avatar Dec 15 '15 09:12 tobrun

To make this a bit more concrete:

Concurrent InfoWindows from #3127 and UserLocationView showcased that we are able to sync Android Views with the underlying MapView

The syncing is performed in these following lines of code:

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surface) {
            mCompassView.update(getDirection());
            mUserLocationView.update();
            for (InfoWindow infoWindow : mInfoWindows) {
                infoWindow.update();
            }
        }

Instead of updating these Views one by one, I would like to iterate a List<View> an update every entry. An entry will be updated based on type of interface implemented by the developer. This interface is responsible for defining update behaviour (eg. following a certain LatLng, expose an update method, ... etc.). We than can also expose add/remove methods for end developers.

tobrun avatar Jan 11 '16 17:01 tobrun

Been doing some tests to see the performance hit when adding new annotations. All benchmarks have been run on minZoom level with some panning.

  • No markers

device-2016-04-05-111732

  • 1 marker

device-2016-04-05-110616

  • 10 markers

device-2016-04-05-111930

  • 20 markers

device-2016-04-05-112107

  • 36 markers

device-2016-04-05-111538

  • +100 markers

device-2016-04-05-110328

tobrun avatar Apr 05 '16 09:04 tobrun

Previous test was using a basic View with only a background. I'm thinking about 3 use cases that needs some extra investigation. Bigger size views, a more complex ViewHierarchy and animated Views.

tobrun avatar Apr 05 '16 09:04 tobrun

Bigger size views

  • 20 markers 12px

device-2016-04-05-112640

  • 20 markers 24px

device-2016-04-05-112107

  • 20 markers 48px device-2016-04-05-112431

Conclusion

There is no clear relation between the size of the View and GPU performance.

tobrun avatar Apr 05 '16 09:04 tobrun

more complex ViewHierarchy

  • no extra parent viewgroup

device-2016-04-05-112107

  • 1 extra parent viewgroup

device-2016-04-05-113334

  • 3 extra parent viewgroup

device-2016-04-05-113425

Conclusion

It adds some extra overhead having a more complex View hierarchy.

tobrun avatar Apr 05 '16 09:04 tobrun

Complex View

  • View with color background

device-2016-04-05-114227

  • TextView with text and color background device-2016-04-05-113917
  • ImageView with icon and color background device-2016-04-05-114423

Conclusion

There is a clear relation between the decrease in performance when showing a more complex view.

tobrun avatar Apr 05 '16 09:04 tobrun

Animations

1 alpha animation with 20 view markers

device-2016-04-05-115938

10 alpha animations with 20 view markers

device-2016-04-05-115742

20 alpha animations with 20 view markers

device-2016-04-05-115457

Conclusion

Even having 1 animated View marker results in dropping frames

tobrun avatar Apr 05 '16 10:04 tobrun

FWIW: a test with same view animation without a MapView also drops in dropping frames occasionally: device-2016-04-05-125223 device-2016-04-05-125236

tobrun avatar Apr 05 '16 10:04 tobrun

The iOS/OS X analogue to this issue is #1784.

1ec5 avatar Apr 12 '16 18:04 1ec5

There is some internal discussion going on how we are going to expose this. On Android we are not committing to a public API just yet and we are following the solution iOS is taking.

Before looking into #4694 / #4384, I'm currently thinking about taking #2835 for a spin and see if it improves any use cases shown above.

tobrun avatar Apr 18 '16 09:04 tobrun

I have been looking at the WIP branch from @boundsj. What I'm noticing, which is different from my initial idea, is that a platform annotation is associated to the same model as a gl annotation. I'm going for a similar approach here. One difference will be the usage of MarkerView naming instead of AnnotationView (Gmaps vs Mapkit naming).

tobrun avatar Apr 21 '16 08:04 tobrun

Been looking into animation support by applying the basic 4 animators Android SDK exposes:

  • Translate
  • Alpha
  • Rotate
  • Scale

This resulted in following gif:

ezgif com-video-to-gif 4

When the map is idle this works great, only when you start panning the map, you see the following:

ezgif com-video-to-gif 5

There is an issue with the translate animator because it with not use the current x,y position but will calculate the interpolated values from what you have passed initially. I don't really consider this an issue, since users will be able to use a LatLng animator instead as implemented in AnimatedMarkerActivity.

tobrun avatar Apr 21 '16 11:04 tobrun

Next up is looking into how we are going to expose this as an API.

On top of my head these are the current options:

  • setter through MarkerOptions
  • setter through Marker
  • adapter approach similar to InfoWindowAdapter
  • ...

MarkerOptions

The problem with integration into the Marker builder is that the members must be Parceable which an Android SDK View isn't. Currently no going to look any further into this.

Marker

What you see in previous comment is done through Marker.setMarkerView but I'm personally not big fan of this setup because it defeats the purpose of having a builder pattern. It's also not very visible or intuitive for the end developer.

Adapter

I'm currently thinking about applying an adapter pattern. At some point we could implement something as a View pool and adapt Views from that pool to a marker on the screen. This requires that we at some point release an adapted view back to the pool. In other words we should create a system that only shows a View if it's found in current viewport, if not it should be released to the View pool. This would be a big win for performance since we can recycle views instead of constantly inflating new ones+ we are also not required to update out of viewport view annotations. Both these arguments could become requirements when marker clustering lands.

At this point I'm just thinking about providing an interface as public API that at some point could be converted to an adapter approach. The downside for this simplistic approach is that the interface will be called instantly when adding a marker.

Note: From what I have seen in the iOS code is that they apply a delegation pattern. I feel the adapter approach would result in a similar experience for the end developer.

tobrun avatar Apr 21 '16 12:04 tobrun

While implementing the Adapter approach I initially provided a flag in MarkerOptions to indicate if a Marker is a ViewMarker (syntax: new MarkerOptions().viewMarker(true)). This will enforce that only ViewMarkers will receive a the adapter callback. Now I'm thinking about removing this because intent for end developer is not clear + smells like a premature optimisation. Might revisit this later.

tobrun avatar Apr 21 '16 13:04 tobrun

I'm currently thinking about applying an adapter pattern.

+1 for the pattern, it also brings consistency with other elements of the SDK (e.g. InfoWindow).

zugaldia avatar Apr 21 '16 14:04 zugaldia

When implementing the simplistic adapter approach, I'm using:

      // adapt GL-marker to View-marker
        mapboxMap.setMarkerViewAdapter(new MapboxMap.MarkerViewAdapter() {
            @Nullable
            @Override
            public MarkerView getView(@NonNull Marker marker) {
                if (marker.equals(brazil)) {
                    return createMarkerView("br", R.drawable.ic_brazil);
                } else if (marker.equals(germany)) {
                    return createMarkerView("de", R.drawable.ic_germany);
                } else if (marker.equals(china)) {
                    return createMarkerView("ch", R.drawable.ic_china);
                } else if (marker.equals(us)) {
                    return createMarkerView("us", R.drawable.ic_us);
                }
                return null;
            }
        });

        // add markers
        china = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(31.230416, 121.473701)));
        brazil = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(-15.798200, -47.922363)));
        us = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(38.907192, -77.036871)));
        germany = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(52.520007, 13.404954)));

But this is currently not usable since the MarkerViewAdapter.getView is called inside the addMarker method so I can't compare markers in getView since those markers are non existent at that time.

update: the only good solution to workaround this issue would be to properly adapt views based on the markers shown on screen with getAnnotationsInBounds in conjuction with getVisibleBounds. This would result in calling MarkerViewAdapter.getView at a later time.

tobrun avatar Apr 21 '16 14:04 tobrun

I touched base briefly with @boundsj and he indicated iOS is moving towards the same solution (View reuse). The difficult part is how we are going to be aware of Marker entering and leaving the screen, a couple of possible solutions can be found in #4694 / #4384.

tobrun avatar Apr 22 '16 11:04 tobrun

I'm back running at this. Been looking at progress on iOS side in https://github.com/mapbox/mapbox-gl-native/pull/4801/commits/4db0d0f82352c453ff47fae6fcf6b66f078404ec and seeing that they are updating the view annotations as part of a map change event:

case mbgl::MapChangeRegionIsChanging:
    ....
           [self updateAnnotationViews];
    ....            
}            

More details about the updateAnnotationsViews method can be found here

Next steps for android is getting a similar system in place that will track which markers are currently visible in the viewport. On android this could give some performance overhead since we need to pass through jni to get the view annotations in bounds. Once we know which are found in the viewport and which aren't we can look into applying the view reuse/adapter approach.

tobrun avatar Apr 27 '16 08:04 tobrun