javaandroidmapboxcallouts

set callout image mapbox android


in iOS you can easily set a callout for your markers by calling:

[marker setCanShowCallout:YES];
[marker setRightCalloutAccessoryView:YOUR_BUTTON];

But I can't find this functionality for the Mapbox Android SDK. I do have a listener now which detects touches on the calloutview but how can i set a callout image / button?

Marker marker = new Marker(p.getTitle(), p.getCatagoryName(), new LatLng(p.getLatitude(), p.getLongitude()));
                        marker.setMarker(getResources().getDrawable(getResources().getIdentifier(string, "drawable", getActivity().getPackageName())));
                                mMapView.addMarker(marker);

                        InfoWindow toolTip = marker.getToolTip(mMapView);
                        View view = toolTip.getView();
                        // view.setBackgroundResource(R.drawable.callout_button); THIS DOES NOT WORK
                        view.setOnTouchListener(new View.OnTouchListener() {
                            @Override
                            public boolean onTouch(View view, MotionEvent motionEvent) {
                                Log.e(TAG, "onTouch");

                                return true;
                            }
                        });

Solution

  • ryansh’s excellent response required that I include TextViews with ids tooltip_title and tooltip_description in the custom layout. I also had to add a third tooltip_subdescription TextView. The default InfoWindow code assumes these views exist and will crash if they don’t. For more control I extended InfoWindow, overrode onOpen and was able to use whatever layout I wanted for the tooltip. For the overridden createTooltip in the extended Marker class, I naturally instantiated and returned my extended InfoWindow object.

    Update. Here is an example of extending the Marker & InfoWindow to support custom tooltip:

    public class MapboxMarker extends Marker {
        private MyInfoWindow mInfoWindow;
    
        public class MyInfoWindow extends InfoWindow {
    
            public MyInfoWindow(int layoutResId, MapView mapView) {
                super(layoutResId, mapView);
            }
    
            public void onOpen(Marker overlayItem) {
                //
                //  Set data on mInfoWindow.getView()
                //
            }
    
        }
    
        public MapboxMarker(MapView mv, LatLng aLatLng, MapController mapController){
            super(mv, "Title", "Description", aLatLng);
        }
    
        @Override
        protected InfoWindow createTooltip(MapView mv) {
            mInfoWindow = new MyInfoWindow(R.layout.custom_tooltip_layout, mv);
            return mInfoWindow;
        }
    }