Domanda

I anticipate using panBy but I'm not sure how to detect when mouse is "close" to the bounding box. Not even sure where to start here.

È stato utile?

Soluzione

Ok, here's how I ended up doing it.

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

There may be a more efficient way to do this, but nobody has said anything, and this works, so it will do.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top