【问题标题】:Listen to My Location icon tap event in Google Maps API v2在 Google Maps API v2 中收听我的位置图标点击事件
【发布时间】:2013-01-17 16:21:04
【问题描述】:

对于新的 Maps API,我注意到当您在地图上按下“我的位置”图钉时,onMapClick()onMarkerClick() 都不会触发。

这表明我应该在某个地方有一个监听器来监听这些事件,在这些事件中按下“我的位置”触摸事件,但我找不到。有人对此有解决方案吗?

我一直在考虑这样的解决方法:

禁用标准的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. 我不知道如果我替换默认的LocationSource,实现我自己的onLocationListener 是否会破坏任何标准功能。在上面找不到任何文档或源代码。

【问题讨论】:

  • 如果您不想要额外的标记功能,也可以使用GroundOverlay,并使用onMapClick() 回调。

标签: android google-maps geolocation listener


【解决方案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 ... */
        }
    }
}

【讨论】:

    【解决方案2】:

    “破坏任何东西”是什么意思?能详细点吗?

    另一种选择,您也可以尝试从系统位置服务中监听位置变化。在您的活动中请求位置服务:

    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)); 
    }
    

    【讨论】:

    • 好吧,GoogleMap 有一个默认的LocationSource,如果我用我自己的替换它可能会丢失它的功能。我不知道如何覆盖它,因为无法找出它是什么类,并且GoogleMap 没有getLocationSource() 方法。
    • 是的,我同意你的看法。似乎没有直接的方法来获取默认位置源。但是,如果我没记错的话,您的目标是在您的位置标记上捕捉触摸事件。只要您可以保留对标记的引用,您的问题就会解决。我认为使用系统定位服务(或实现您自己的定位源)没有任何缺点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 2012-12-22
    • 2013-08-27
    相关资源
    最近更新 更多