gpt4 book ai didi

javascript - 谷歌地图在页面宽度上更改偏移中心

转载 作者:行者123 更新时间:2023-11-28 04:21:26 29 4
gpt4 key购买 nike

我正在尝试制作一个 design我的代码没有像我预期的那样工作..如果旧宽度大于新宽度并且偏移中心 -50如果旧宽度小于新宽度,则偏移中心 50

偏移中心与 if 一起工作但这意味着如果用户调整窗口大小,每次用户调整大小时偏移中心将为 50 或 -50

map 上的矩形只是为了覆盖名字网页格式

CSS

#map{
width: 100%;
height: 400px;
}

js

<script async defer
src="https://maps.googleapis.com/maps/api/js?key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&callback=initMap">
</script>


<script>
function oh_ow() {
var oh = $(window).height();
var ow = $(window).width();
return ow;
}
var old_w = oh_ow()
alert(old_w );

alert(ow);
function initMap() {
var uluru = {lat: 62.26393, lng: 82.386004};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 16,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});



function offsetCenter(latlng, offsetx, offsety) {

// latlng is the apparent centre-point
// offsetx is the distance you want that point to move to the right, in pixels
// offsety is the distance you want that point to move upwards, in pixels
// offset can be negative
// offsetx and offsety are both optional

var scale = Math.pow(2, map.getZoom());

var worldCoordinateCenter = map.getProjection().fromLatLngToPoint(latlng);
var pixelOffset = new google.maps.Point((offsetx/scale) || 0,(offsety/scale) ||0);

var worldCoordinateNewCenter = new google.maps.Point(
worldCoordinateCenter.x - pixelOffset.x,
worldCoordinateCenter.y + pixelOffset.y
);

var newCenter = map.getProjection().fromPointToLatLng(worldCoordinateNewCenter);

map.setCenter(newCenter);

}

google.maps.event.addDomListener(window, 'resize', function() {

//---------------------------------------------
var rtime;
var timeout = false;
var delta = 200;
$(window).resize(function() {
rtime = new Date();
if (timeout === false) {
timeout = true;

setTimeout(resizeend, delta);
}
});
function resizeend() {
if (new Date() - rtime < delta) {
setTimeout(resizeend, delta);
} else {
timeout = false;
var nh = $(window).height();
var nw = $(window).width();
//alert("old"+old_w);
//alert("new"+nw);
if(nw > old_w){
offsetCenter(map.getCenter(),50,-0);
}else if(nw < old_w){
offsetCenter(map.getCenter(),-50,-0);
}


}
}
//---------------------------------------------
});





}



</script>

我不会是一个 react 灵敏的处女How to offset the center point in Google maps api V3

有人能帮忙吗

最佳答案

据我了解,我认为可以使用setCenterpanTopanBy 方法来实现。

function initMap() {
var uluru = {
lat: 25.26393,
lng: 55.386004
};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 16,
center: uluru
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});

var winWidth = $(window).width();
panMap();

$(window).resize(function() {
map.setCenter(uluru);
panMap();
});

google.maps.event.addListener(map, 'zoom_changed', function() {
map.panTo(uluru);
panMap();
});

function panMap(xChange){
var winWidth = $(window).width();
if(winWidth > 671){
map.panBy(xChange || -170, 0);
}
}

}

您可以调整 -170 以获得所需的位置。

此处演示:https://jsbin.com/fasawofofa/1/edit?html,output

关于javascript - 谷歌地图在页面宽度上更改偏移中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42186464/

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