簡體   English   中英

谷歌android映射api v2顯示標記標題始終

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

我在我的Android應用程序中有一個谷歌地圖v2和一些標記。 當用戶單擊其中一個標記時,會出現標題彈出窗口。 如何在沒有用戶點擊的情況下顯示這些標題?

像這樣的東西:

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

你可以這樣做:

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

為了在標記周圍的地圖上顯示標記標題,我在互聯網上找到的解決方案都沒有為我工作,所以我卷起袖子並制作了這個庫來解決這個問題,希望它也會幫助其他人:

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

以下是其工作原理的預覽:

預習

我在某些實現方面不太好,但這是我的代碼:

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

我在一個地圖應用程序的片段中實現了這一點,並帶有一些變化 例如,使用自定義信息窗口和地圖上的某些事件,但要求其實現同時顯示所有標記標題。 如果它對你有幫助,請告訴我

“activity.getResources()。getDimensionPixelSize(R.dimen.tooltips_width)”的ACTIVITY“變量是片段中的屬性

也許我在回答這個問題時有點遲,但下面的代碼對我有用:

//添加這個庫

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

//然后使用下面的方法

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