简体   繁体   English

谷歌android映射api v2显示标记标题始终

[英]Google android maps api v2 Show Marker Title Always

I have a google maps v2 in my android application and some markers on it. 我在我的Android应用程序中有一个谷歌地图v2和一些标记。 When user click one of these markers, a title popup comes. 当用户单击其中一个标记时,会出现标题弹出窗口。 How can I show these titles always without user click? 如何在没有用户点击的情况下显示这些标题?

Something like that: 像这样的东西:

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

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 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 “activity.getResources()。getDimensionPixelSize(R.dimen.tooltips_width)”的ACTIVITY“变量是片段中的属性

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")));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM