我在我的网站中集成了 Google map ,但 map 未在我的网站中加载。但是当我打开检查元素 map 时,检查元素 map 将起作用,当我关闭时,检查元素 map 将自动消失。我不知道为什么会这样,请让我知道是什么问题。这是我的 map 代码
提前致谢
<div id="mapCanvas" style="width:100%; height:400px"></div>
<input type="text" title="Latitude" id="latt_val">
<input type="text" title="Longitude" id="long_val">
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
function geocodePosition(pos) {
geocoder.geocode({
latLng: pos
}, function(responses) {
if (responses && responses.length > 0) {
updateMarkerAddress(responses[0].formatted_address);
} else {
updateMarkerAddress('Cannot determine address at this location.');
}
});
}
function updateMarkerStatus(str) {
document.getElementById('markerStatus').innerHTML = str;
}
function updateMarkerPosition(latLng) {
document.getElementById('info').innerHTML = [
latLng.lat(),
latLng.lng()
].join(', ');
document.getElementById("latt_val").value = latLng.lat();
document.getElementById("long_val").value = latLng.lng();
}
function updateMarkerAddress(str) {
document.getElementById('address').innerHTML = str;
}
function initialize() {
var latLng = new google.maps.LatLng(15.486432009100538, 73.82759375);
var map = new google.maps.Map(document.getElementById('mapCanvas'), {
zoom: 12,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: latLng,
title: 'Point A',
map: map,
draggable: true
});
// Update current position info.
updateMarkerPosition(latLng);
geocodePosition(latLng);
// Add dragging event listeners.
google.maps.event.addListener(marker, 'dragstart', function() {
updateMarkerAddress('Dragging...');
});
google.maps.event.addListener(marker, 'drag', function() {
updateMarkerStatus('Dragging...');
updateMarkerPosition(marker.getPosition());
});
google.maps.event.addListener(marker, 'dragend', function() {
updateMarkerStatus('Drag ended');
geocodePosition(marker.getPosition());
});
google.maps.event.trigger(window,'resize',{});
}
// Onload handler to fire off the app.
google.maps.event.addDomListener(window, 'load', initialize);
</script>
最佳答案
[更新]
嘿,我还是很好奇这里发生了什么。您的浏览器是否启用了可能会阻止 map 加载的广告拦截器?您是否检查过这是否适用于多个浏览器?
我用代码笔解决了我们的问题:http://codepen.io/trevorwhealy/pen/obRQdW 。 而且你的 map 工作得很好。如果在加载这支笔时,您仍然看不到某些内容,那么我想我们可以有把握地说这是浏览器问题。
[旧帖子]
尝试输入您的代码:
google.maps.event.trigger(window,'resize',{}); // or trigger(map
之后
google.maps.event.addListener(marker, 'dragend', function() {
updateMarkerStatus('Drag ended');
geocodePosition(marker.getPosition());
});
但仍在初始化函数内。
关于javascript - 打开浏览器时未加载谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35453628/