2012-12-12 153 views
17

我在我的應用程序中爲用戶的當前位置使用自定義圖標,並且希望在升級到新的Google地圖庫時保持此方式。Android谷歌地圖V2更改我的位置圖標

藉助Google Maps v1庫,我擴展了MyLocationOverlay並覆蓋了drawMyLocation方法以在其中繪製自定義圖標。

據我所知,GoogleMap使用setMyLocationEnabled方法啓用當前位置,但無法自定義它。

有人知道如何在v2上完成此操作嗎?

回答

3

好了,我只是想,如果你不添加標題或摘錄,標記不會對點擊事件的影響,以及它作爲行爲一個圖像覆蓋。雖然它並不完美,它適合我的需要

private Marker marker = mMap.addMarker(new MarkerOptions() 
        .position(latLng) 
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow))); 

謝謝大家

13

在使用自定義圖標的地圖構造函數中創建標記。

_myLocation = mMap.addMarker(new MarkerOptions() 
         .position(MAP_CENTER) 
         .title("My Location") 
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.mylocation))); 

當提供被稱爲實現改變監聽器位置, https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/LocationSource.OnLocationChangedListener

更新標記位置:

public void onLocationChanged (Location location) 
{ 
    _myLocation.position(location); //May have to convert from location to LatLng 
} 
+1

這是它被用來進行的V1庫做的方式。在V2中,您不會在活動中處理MapView。而且,V2上沒有Overlay類可以擴展。 https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/package-summary –

+1

我的錯誤,我沒有意識到您的意思是Google Play版本的地圖。請參閱上面我更新的答案。這很好,因爲它現在與Google Maps Javascript版本的行爲非常相似。 – javram

+1

@RobertEstivill:AFAIK,這是你唯一的選擇,至少在堅持記錄/支持的行爲時。 – CommonsWare

0

肯定是有一種方式來顯示當前位置的自定義圖標。 結帳Link你會得到更多的想法來定製你的地圖上的圖標。

嘗試下面的代碼:

private static final LatLng MELBOURNE = new LatLng(-37.81319, 144.96298); 
private Marker melbourne = mMap.addMarker(new MarkerOptions() 
         .position(MELBOURNE) 
         .title("Melbourne") 
         .snippet("Population: 4,137,400") 
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow))); 
+0

這可以完成,但它與當前MyLocation機制不兼容。換句話說,您可以使用不帶所有窗口信息的標記,但是您必須手動實施LocationListener來移動您繪製的位置。您不能重複使用內置的位置功能 –

+0

這是AFAIK的唯一方法。請張貼一些代碼,以便我可以瞭解您的問題。謝謝。 – GrIsHu