2

目前,默认情况下,当点击标记时,地图以标记为中心。有没有办法控制它,引入一些偏移值。我有一个弹出式信息窗口,它有时会更高一些,我想定位地图,这样它就不会在顶部被切断。

4

2 回答 2

3

您可能会覆盖 GoogleMap 标记单击事件并在那里调整相机。

例如

Maker lastOpened = null;

mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
    public boolean onMarkerClick(Marker marker) { 
        // Check if there is an open info window
        if (lastOpened != null) {
            // Close the info window
            lastOpened.hideInfoWindow();

            // Is the marker the same marker that was already open
            if (lastOpened.equals(marker)) {
                // Nullify the lastOpened object
                lastOpened = null;
                // Return so that the info window isn't opened again
                return true;
            } 
        }

        // Open the info window for the marker
        marker.showInfoWindow();
        // Re-assign the last opened such that we can close it later
        lastOpened = marker;

        // Get the markers current position
        LatLng curMarkerPos = marker.getPosition();

        // Use the markers position to get a new latlng to move the camera to such that it adjusts appropriately to your infowindows height (might be more or less then 0.3 and might need to subtract vs add this is just an example)
        LatLng camMove = new LatLng(curMarkerPos.latitude + 0.3, curMarkerPos.longitude);

        // Create a camera update with the new latlng to move to            
        CameraUpdate camUpdate = CameraUpdateFactory.newLatLng(camMove);
        // Move the map to this position
        mMap.moveCamera(camUpdate);

        // Event was handled by our code do not launch default behaviour.
        return true;
    }
});

mMap.setOnMapClickListener(new OnMapClickListener() {

    @Override
    public void onMapClick(LatLng point) {
        if (lastOpened != null) {
            // Hide the last opened
            lastOpened.hideInfoWindow();

            // Nullify lastOpened
            lastOpened == null;
        }

        // Move the camera to the new position
        final CameraPosition cameraPosition = new CameraPosition.Builder().target(point).zoom(mMap.getCameraPosition().zoom).build();

        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    });

此代码尚未经过测试,但至少应该让您有一个很好的开始。onMarkerClick 的默认行为是移动相机并打开信息窗口。因此,覆盖这一点并实施您自己的应该允许您将相机移动到您喜欢的地方。

谢谢, 曼

于 2013-01-25T19:47:35.080 回答
3

对于只想删除标记单击的移动到中心默认行为的任何人,我修改了上面非常好的答案:

将此添加到 setUpMap():

mMap.setOnMarkerClickListener(getMarkerClickListener());

然后添加方法:

Marker lastOpened = null;

public OnMarkerClickListener getMarkerClickListener()  {
    return new OnMarkerClickListener()  {
        public boolean onMarkerClick(Marker marker) { 
            if (lastOpened != null) {
                lastOpened.hideInfoWindow();
                if (lastOpened.equals(marker)) {
                    lastOpened = null;
                    return true;
                } 
            }

            marker.showInfoWindow();
            lastOpened = marker;
            return true;
        }
    };
}
于 2013-02-06T03:48:57.603 回答