2013-01-17 165 views
3

對於新的地圖API我的位置圖標輕敲事件時,我注意到,無論是onMapClick()也不onMarkerClick()火災時,你按我的位置引腳在地圖上。聽在谷歌地圖API第2

這表明,我認爲應該有一個監聽的地方,聽這些事件在我的位置觸摸事件被按下,但我無法找到任何。有沒有人有這個解決方案?

我一直在思考這樣一個解決方法:

禁用標準MyLocation圖標,並創建一個普通標記,像我的位置引腳功能。

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

和實施LocationSource.OnLocationChangedListener

public void onLocationChanged (Location location) { 
    myLocationPin.position(new LatLng(location.latitude, location.longitude)); 
} 

但我有兩個問題的方法:

  1. 我不知道如何關閉標記關閉
  2. 我不t知道如果我更換默認LocationSource是否實施我自己的onLocationListener將打破任何標準功能。找不到任何文檔或源代碼。
+0

如果您不想使用額外的標記功能並使用'onMapClick()'回調函數,您也可以使用'GroundOverlay'。 – iagreen

回答

1

因爲這個問題被張貼,OnMyLocationChangeListener加入到API,使得解決方法(很多)更容易的實現(即不需要自定義LocationSource)。

因此,您可以在我的位置點頂部繪製Marker(帶有類似圖標),以便接收相應的onMarkerClick()回調。

public class DemoMapFragment extends SupportMapFragment implements OnMyLocationChangeListener, OnMarkerClickListener { 

    // Note that 'mMap' may be null if the Google Play services APK is not available. 
    private GoogleMap mMap; 
    private Marker myLocationMarker; 
    private static BitmapDescriptor markerIconBitmapDescriptor; 
    /* ... */ 

    @Override 
    public void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); // Get a reference to the map  
     mMap.setMyLocationEnabled(true); // Enable the my-location layer 
     mMap.setOnMyLocationChangeListener(this); 
     mMap.setOnMarkerClickListener(this); 
    } 

    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the map. 
     if (mMap == null) { 
      mMap = getMap(); 
      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 
       // The Map is verified. It is now safe to manipulate the map: 

       // Load custom marker icon 
       markerIconBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.my_location_dot_icon); 

       // When the map is first loaded we need to add our marker on top of My Location dot 
       myLocationMarker = mMap.addMarker(new MarkerOptions() 
         .position(new LatLng(mMap.getMyLocation().getLatitude(),mMap.getMyLocation().getLongitude())) 
         .icon(markerIconBitmapDescriptor)); 

       // Set default zoom 
       mMap.moveCamera(CameraUpdateFactory.zoomTo(15f)); 
      } 
     } 
    } 

    @Override 
    public void onMyLocationChange(Location location) { 
     // Remove the old marker object 
     myLocationMarker.remove(); 

     // Add a new marker object at the new (My Location dot) location 
     myLocationMarker = mMap.addMarker(new MarkerOptions() 
       .position(new LatLng(location().getLatitude(),location().getLongitude())) 
       .icon(markerIconBitmapDescriptor)); 
    } 

    @Override 
    public boolean onMarkerClick(Marker marker) { 
     if (marker.equals(myLocationMarker)) { 
      /* My Location dot callback ... */ 
     } 
    } 
} 
0

這是什麼意思「破壞任何東西」?你能詳細說明嗎?

另一種方法是,您也可以嘗試從系統位置服務偵聽位置更改。在您的活動要求的位置服務:

locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 
locatoinManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000,2f,this); 

您的活動延長LocationListener的

public void onLocationChange(Location location){ 
    yourLocationPin.position(new LatLng(location.latitude, location.longitude)); 
} 
+0

好吧,'GoogleMap'有一個默認'LocationSource',它可能具有我可能失去的功能,如果我用我自己的替換它。我不知道如何覆蓋它,因爲沒有辦法找出它是什麼類,而'GoogleMap'沒有'getLocationSource()'方法。 – Maarten

+0

是的,我同意你的意見。似乎沒有直接的方法來獲取默認位置源。但是,如果我沒有錯,您的目標是在您的位置標記上捕捉觸摸事件。只要你可以參考你的標記,你的問題就可以理清。我沒有看到使用系統位置服務(或實現您自己的位置源)的任何缺點。 – GingerJim