2013-02-08 27 views
23

我知道這個主題有一些線程,但我還沒有找到一個足夠好的解決方案。谷歌地圖v2標記zOrdering - 設置頂部

我需要在所有其他標記的頂部放置標記。我該怎麼做呢?

我曾嘗試在所有其他標記之前和之後添加此標記,但排序似乎是從地圖視圖的底部(正面)到頂部(背面)。這是不可接受的,因爲我想集中在所有標記之前的標記。

我知道多邊形可以被調整,但我會更喜歡一個很好風格的圖形!

事實上,您不能在標記上訂購多邊形! 「

」該多邊形相對於其他疊加層繪製的順序,包括多義線,GroundOverlays和TileOverlay,但不包括標記。具有較大z-索引的疊加層繪製在具有較小z-索引的疊加層上。具有相同z-index值的疊加層是任意的,默認值爲0.「 - https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Polygon

有沒有人有任何想法呢?

回答

1

雖然不是完美的解決方案!我已經想出瞭如何通過消耗onMarkerClick事件來顯示所有其他標記的選定(點擊)標記。返回true將消耗該事件,所以我們要做的showInfoWindow和縮放到中心

@Override 
    public boolean onMarkerClick(Marker marker) { 

     //Manually open the window 
     marker.showInfoWindow(); 

     //Animate to center 
     sMapFrag_v2.getMap().animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()); 

     //Consume the method 
     return true; 
    } 
40

艾登弗萊的答覆工作,如果你實際顯示信息窗口。如果你不這樣做,標記將不會出現在前面。這是一個黑客,使之走到前面反正:

創建顯示0dp信息窗口自定義InfoWindowAdapter:

public class MapWindowAdapter implements GoogleMap.InfoWindowAdapter { 
    private Context context = null; 

    public MapWindowAdapter(Context context) { 
     this.context = context; 
    } 

    // Hack to prevent info window from displaying: use a 0dp/0dp frame 
    @Override 
    public View getInfoWindow(Marker marker) { 
     View v = ((Activity) context).getLayoutInflater().inflate(R.layout.no_info_window, null); 
     return v; 
    } 

    @Override 
    public View getInfoContents(Marker marker) { 
     return null; 
    } 
} 

下面是佈局的代碼:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="0dp" 
       android:layout_height="0dp"> 
</LinearLayout> 

設置地圖時,設置適配器和自定義InfoWindowAdapter和OnMarkerClickListener(使用showInfoWindow並按照AIden Fry的建議返回true):

mMap.setInfoWindowAdapter(new MapWindowAdapter(this)); 

//Call showInfoWindow to put marker on top of others. 
myMarker.showInfoWindow(); 

這並不美麗,但我沒有找到任何其他方式來處理z-indexing而不使用信息窗口。

+0

這是正確的答案。對於這樣的基本任務必須做到骯髒的黑客真是太遺憾了。 – lukas 2013-07-17 14:57:09

+0

順便說一下,設置標題不是必需的。 – lukas 2013-07-17 15:03:50

+4

這工作。不過,不需要實現setOnMarkerClickListener。只需按上面所述設置InfoWindowAdapter,然後調用showInfoWindow將標記設置在最前面。 – 2014-02-11 15:02:28

14

這在加入谷歌Play服務9.2June 27, 2016

MarkerOptions.zIndex()設置相對於地圖上的其他標記標記的堆疊順序。詳細瞭解marker z-indexes以及z-index對點擊事件的影響。 (Issue 4688

+0

不幸的是,Xamarin沒有支持這個屬性呢!不相關的問題,但值得一提...... – Justin 2016-11-22 19:30:46

+0

我來到這裏爲Xamarin解決方案,但是說@Justin我沒有看到OP的問題涉及Xamarin具體。我很期待Xamarin將它帶入他們的API。 – 2017-01-13 17:26:27