我在使用 Google map 时遇到问题。我将 map JS 加载到一个文件中,在该文件中使用 ajax(和 jquery)附加一个模态 div,其中包含 map 。我得到的结果如下图所示:
但是,当我“关闭”模式(删除 div,从而删除 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
是一个包含 address
和 title
的对象
最佳答案
版本 2 有一个 checkresize(),我记得在 map 会移动中心点的情况下使用过它。我认为 v3 的等价物是这样的:
google.maps.event.trigger( map , '调整大小');
关于javascript - Google map 看起来在 div 内移动了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12388816/