2017-08-11 110 views
0

我正在使用Google地圖Android API的Android應用程序。我在地圖上顯示多個標記,並且想要在每個標記旁邊顯示一個標籤,如下圖所示,但我無法找出如何去做吧。如何在Android上的Google地圖上的標記旁邊顯示標籤?

Image with google map marker and label

我已經通過谷歌地圖文檔了在https://developers.google.com/maps/documentation/android-api/map-with-marker 但我無法找到如何顯示在這個manner.I標籤細節都試圖開闢了一個彈出,但我title屬性需要一個展示我的標記的詳細視圖(信息窗口),我需要顯示的主標題,而無需打開彈出任何標記

任何想法,我怎麼能以這樣的方式添加標記在附加的圖像顯示?

+1

您可以檢查這些https://stackoverflow.com/questions/17989389/label-at -The-top-of-the-marker-in-google-maps-api-v2 –

+0

@NavjotSingh:我已經看到了這些。標題屬性僅在用戶點擊標記時顯示 –

回答

1

不知道這是你腦子裏想的是什麼,但在這裏不用

創建MarkerOptions對象:

MarkerOptions options = new MarkerOptions() 
       .title(name) 
       .position(source) 
       .icon(gettIconFromDrawable(myMarker)) 
       .snippet("Briefly describe \nyour content here"); 

然後標記添加到地圖

Marker sourceMarker = mMap.addMarker(options); 

創建InfoWindowAdapter以容納摘錄中的多行。 (原來這裏的answer

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 

      @Override 
      public View getInfoWindow(Marker arg0) { 
       return null; 
      } 

      @Override 
      public View getInfoContents(Marker marker) { 

       Context context = getApplicationContext(); //or getActivity(), YourActivity.this, etc. 

       LinearLayout info = new LinearLayout(context); 
       info.setOrientation(LinearLayout.VERTICAL); 

       TextView title = new TextView(context); 
       title.setTextColor(Color.BLACK); 
       title.setGravity(Gravity.CENTER); 
       title.setTypeface(null, Typeface.BOLD); 
       title.setText(marker.getTitle()); 

       TextView snippet = new TextView(context); 
       snippet.setTextColor(Color.GRAY); 
       snippet.setText(marker.getSnippet()); 

       info.addView(title); 
       info.addView(snippet); 

       return info; 
      } 
     }); 

最後,添加該屬性,以保證標記保持可見

sourceMarker.showInfoWindow(); 
+0

感謝您的幫助。但實際上,我在問題中提到我已經使用infoWindow來顯示有關標記對象的自定義和詳細的視圖。我需要爲地圖上的所有標記顯示一行標記,而不點擊標記 –

+0

這就是'sourceMarker.showInfoWindow();'應該做什麼 –

+0

但我的要求稍有不同。所有標記都必須有可見的標籤,並且點擊標記時應該顯示具有該標記詳細信息的詳細信息窗口 –

相關問題