Expose a generic Android View Marker API
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
Related: https://github.com/mapbox/mapbox-gl-native/issues/3116
Related: #1125
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.
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

- 1 marker

- 10 markers

- 20 markers

- 36 markers

- +100 markers

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.
Bigger size views
- 20 markers 12px

- 20 markers 24px

- 20 markers 48px

Conclusion
There is no clear relation between the size of the View and GPU performance.
more complex ViewHierarchy
- no extra parent viewgroup

- 1 extra parent viewgroup

- 3 extra parent viewgroup

Conclusion
It adds some extra overhead having a more complex View hierarchy.
Complex View
- View with color background

- TextView with text and color background

- ImageView with icon and color background

Conclusion
There is a clear relation between the decrease in performance when showing a more complex view.
Animations
1 alpha animation with 20 view markers

10 alpha animations with 20 view markers

20 alpha animations with 20 view markers

Conclusion
Even having 1 animated View marker results in dropping frames
FWIW: a test with same view animation without a MapView also drops in dropping frames occasionally:

The iOS/OS X analogue to this issue is #1784.
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.
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).
Been looking into animation support by applying the basic 4 animators Android SDK exposes:
- Translate
- Alpha
- Rotate
- Scale
This resulted in following gif:

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

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