gpt4 book ai didi

javascript - 谷歌地图看起来移动到 div 中

转载 作者:行者123 更新时间:2023-11-29 18:26:45 24 4
gpt4 key购买 nike

我在使用谷歌地图时遇到问题。我将 map JS 加载到一个文件中,在该文件中我使用 ajax(和 jquery)附加了一个包含 map 的模态 div。类似下图的东西是我得到的:

Correct map

但是,当我“关闭”模态(它删除了 div,从而删除了 map )并尝试再次打开它时,出现了一个问题:

Moved map

除了看起来不完整之外, map 似乎认为其左上角是 div 之外的一些像素。我向你保证这一点,因为当我尝试移动 map 时,它会刷新其内容并删除 map 的“不可见”(不幸的是可见,在这种情况下)部分。我将用于显示 map 的代码留在这里(尽管它非常简单)。

var marker = null, geocoder, latLng, map = null;
geocoder = new google.maps.Geocoder();
latLng = new google.maps.LatLng(41.3865, 2.1648);
var myOptions = {
zoom: 16,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: false
};
if(map === null) {
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

function clearOverlays() {
if (marker !== null) {
marker.setMap(null);
marker = null;
}
}

function getPosition(address, title) {
clearOverlays();

geocoder.geocode({
address : address,
region : 'es'
},
function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
var result = results[0];
latLng = result.geometry.location

marker = new google.maps.Marker({
'title': title,
'map': map,
'position': latLng,
'animation': google.maps.Animation.DROP
});
map.setCenter(latLng);
}
}
);
}

getPosition(place.address, place.title);

为了示例,我们假设 place 是一个包含 addresstitle 的对象

最佳答案

版本 2 有一个 checkresize(),我记得在这种情况下 map 会移动中心点时使用过它。我认为 v3 等价物是这样的:

google.maps.event.trigger(map, 'resize');

关于javascript - 谷歌地图看起来移动到 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12388816/

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