地图V2 myLocation蓝点回调回调、地图、myLocation

2023-09-08 08:41:36 作者:姓白名痴

我希望能够点击蓝点(我的位置),其显示在地图上。反正有从点击得到一个回调?

I want to be able to click the blue dot (my location) that is shown on the map. Is there anyway to get a callback from that click?

谢谢, 马亭

推荐答案

一个可能的解决方法可能被绘制标记(具有类似的图标)上的我的位置上方点这样你就可以得到相应的 onMarkerClick()回调。这也将需要删除的标记,将其添加到新的位置,每次有一个位置更新时,您可以通过执行 OnMyLocationChangeListener 听。

One possible workaround might be drawing a Marker (with a similar icon) on top of the My Location dot so you can receive the corresponding onMarkerClick() callback. This would also require removing the marker and adding it to the new location everytime there's a location update event, which you can listen to by implementing OnMyLocationChangeListener.

修改:在 OnMyLocationChangeListener 接口现在是很precated,应该改用新的 LocationClient 和相关 LocationListener的

EDIT: the OnMyLocationChangeListener interface is now deprecated, one should use instead the new LocationClient and the associated LocationListener.

因此​​,有关code可能是这个样子(我没有实际测试过这一点):

So the relevant code could look something like this (I hadn't actually tested this):

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 ... */
        }
    }
}