1

我预计使用 panBy 但我不确定如何检测鼠标何时“靠近”边界框。甚至不知道从哪里开始。

4

1 回答 1

2

好的,这就是我最终做到的方式。

google.maps.event.addListener(map, 'mousemove', function(event) {

        ///
        var overlay = new google.maps.OverlayView();
        overlay.draw = function() {};
        overlay.setMap(map);

        ///
        var point = map.getCenter();
        var projection = overlay.getProjection();
        var pixelpoint = projection.fromLatLngToDivPixel(point);

        ///
        var thelatlng = event.latLng;
        var proj = overlay.getProjection();
        var thepix = proj.fromLatLngToDivPixel(thelatlng);
        var mapBounds = map.getBounds();
        var mB_NE = mapBounds.getNorthEast();
        var mB_SW = mapBounds.getSouthWest();

        var nE = proj.fromLatLngToDivPixel(mB_NE);
        var sW = proj.fromLatLngToDivPixel(mB_SW);

        var north = nE.y;
        var east = nE.x;
        var south = sW.y;
        var west = sW.x;


        var appx_north, appx_east, appx_south, appx_west = false;
        if (Math.round(thepix.y) <= Math.round(north+20)) {appx_north = true;}
        if (Math.round(thepix.x) >= Math.round(east-20)) {appx_east = true;}
        if (Math.round(thepix.y) >= Math.round(south-20)) {appx_south = true;}
        if (Math.round(thepix.x) <= Math.round(west+20)) {appx_west = true;}

        if (appx_north) {
            pixelpoint.y -= 5;
            point = projection.fromDivPixelToLatLng(pixelpoint);
            map.setCenter(point);
        }
        if (appx_east) {
            pixelpoint.x += 5;
            point = projection.fromDivPixelToLatLng(pixelpoint);
            map.setCenter(point);
        }
        if (appx_south) {
            pixelpoint.y += 5;
            point = projection.fromDivPixelToLatLng(pixelpoint);
            map.setCenter(point);
        }
        if (appx_west) {
            pixelpoint.x -= 5;
            point = projection.fromDivPixelToLatLng(pixelpoint);
            map.setCenter(point);
        }
    });

可能有一种更有效的方法可以做到这一点,但没有人说什么,这有效,所以它会做。

于 2013-02-04T22:09:45.987 回答