2017-02-10 56 views
3

我試圖顏色像這樣在谷歌地圖的一些建築:谷歌地圖文字疊加 - Android電子

enter image description here

我能夠覆蓋在建築物頂部的多邊形對象,但我不是能申請在它上面的一個標籤(建築ID)。

如何在Android上面的圖像中顯示建築物上的文字?

回答

3

我知道這個問題有點舊,但我只是想出瞭如何爲我自己的項目做到這一點,並認爲我會分享。該方法是創建文本的位圖圖像,然後將其顯示爲GroundOverlay,並將其顯示爲zIndex大於多邊形的多邊形。

如果你有LatLngs的單供多邊形,你可以做到以下幾點:

private void showText(List<LatLng> latLngList) { 
    LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
    for (LatLng latLng : latLngList) { 
     builder.include(latLng); 
    } 

    googleMap.addGroundOverlay(new GroundOverlayOptions() 
      .positionFromBounds(builder.build()) 
      .image(
        BitmapDescriptorFactory.fromBitmap(
          getBitmapFromView() 
        ) 
      ) 
      .zIndex(100) 
    ); 
} 

的.zIndex因爲你要顯示在文本(100)的部分是很重要的多邊形。 (我不確定什麼是最大的z索引是100作品)。

下面是將佈局轉換爲位圖的方法。

private Bitmap getBitmapFromView() { 
    View customView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.my_text_layout, null); 
    customView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); 
    customView.layout(0, 0, customView.getMeasuredWidth(), customView.getMeasuredHeight()); 
    customView.buildDrawingCache(); 
    Bitmap returnedBitmap = Bitmap.createBitmap(customView.getMeasuredWidth(), customView.getMeasuredHeight(), 
      Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(returnedBitmap); 
    canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN); 
    Drawable drawable = customView.getBackground(); 
    if (drawable != null) { 
     drawable.draw(canvas); 
    } 
    customView.draw(canvas); 
    return returnedBitmap; 
} 

如果你只是想文本,那麼你的佈局「R.layout.my_text_layout」可以只與一個TextView的佈局。