gpt4 book ai didi

javascript - Google Maps v3 API - 当鼠标靠近边界框时如何平移 map

转载 作者:行者123 更新时间:2023-11-30 18:10:40 24 4
gpt4 key购买 nike

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

最佳答案

好的,这就是我最终的做法。

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

可能有更有效的方法来做到这一点,但没有人说什么,而且这个有效,所以它会起作用。

关于javascript - Google Maps v3 API - 当鼠标靠近边界框时如何平移 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14634691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com