Google android maps api v2 Show Marker Title Always

泪湿孤枕 提交于 2019-11-28 21:06:26
José De la O

Something like that:

googleMap.addMarker(new MarkerOptions()
    .position(new LatLng(latitude, longitude))
    .title("Your position")).showInfoWindow();
Khalil_Java

You can do this:

Marker marker = mMap.addMarker(new MarkerOptions().position(currentPosition).title("Your text"));
marker.showInfoWindow();

For displaying marker titles on the map around the markers, none of the solutions I found on internet worked for me so I rolled up my sleeves and made this library which solves this problem for me, hopefully it will help others too:

https://github.com/androidseb/android-google-maps-floating-marker-titles

Here is a preview of how it works:

I'm not so good in some implementation but here is my code:

public BitmapDescriptor getBitmapFromView(String title) {
        View view = LayoutInflater.from(activity.getApplicationContext()).inflate(R.layout.marker_tooltip, null);
        ((TextView) view.findViewById(R.id.tooltips_title)).setText(title);

        //Get the dimensions of the view. In my case they are in a dimen file
        int width = activity.getResources().getDimensionPixelSize(R.dimen.tooltips_width);
        int height = activity.getResources().getDimensionPixelSize(R.dimen.tooltips_height);

        int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);

        view.measure(measuredWidth, measuredHeight);
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        view.draw(canvas);

        return BitmapDescriptorFactory.fromBitmap(bitmap);
    }

...
marker.setIcon(getBitmapFromView(marker.getTitle()));
...

I implemented this in a fragment in a map app with some variations. For example, with a custom Info Window and some events on the map but its implementation was required to show all marker title at the same time. Let me know if it was helpful for you too

the ACTIVITY of "activity.getResources().getDimensionPixelSize(R.dimen.tooltips_width)" variable is a property in the fragment

Maybe I am a bit late in answering this one but the code below works for me:

//add this library

implementation 'com.google.maps.android:android-maps-utils:0.5'

//then use the method below

public void makersMaker(GoogleMap googleMap){
    IconGenerator iconFactory = new IconGenerator(this);                            
    Marker marker1 = googleMap.addMarker(new MarkerOptions().position(newLatLng(-1.3177336,36.8251902));                  
    marker1.setIcon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon("Marker 1")));
    Marker marker2 = googleMap.addMarker(new MarkerOptions().position(new LatLng(-1.2857399,36.8214088)));                
    marker2.setIcon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon("Marker 2")));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!