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
feature Android