Javascript 谷歌地图地理代码在模式下不工作
谷歌地图地理编码功能在我的模式中不起作用 我试过在索卢昂下面 但它返回未定义的映射错误 我也试过这个 比我想的要多 但它仍然不起作用,我的代码如下Javascript 谷歌地图地理代码在模式下不工作,javascript,google-maps-api-3,bootstrap-modal,Javascript,Google Maps Api 3,Bootstrap Modal,谷歌地图地理编码功能在我的模式中不起作用 我试过在索卢昂下面 但它返回未定义的映射错误 我也试过这个 比我想的要多 但它仍然不起作用,我的代码如下 <script> var map; function initMap() { var map = new google.maps.Map(document.getElementById('map'), { zoom: 5, center: {lat: -34.3
<script>
var map;
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: {lat: -34.397, lng: 150.644}
});
var geocoder = new google.maps.Geocoder();
geocodeAddress(geocoder, maps);
}
function geocodeAddress(geocoder, resultsMap) {
var address = "Ahmedabad India";
geocoder.geocode({'address': address}, function(results, status) {
if (status === 'OK') {
resultsMap.setCenter(results[0].geometry.location);
var contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">Websters House</h1>'+
'<div id="bodyContent">'+
'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
'Aboriginal people of the area. It has many springs, waterholes, '+
'rock caves and ancient paintings. Uluru is listed as a World '+
'Heritage Site.</p>'+
'<p>Attribution: Uluru, <a href="https://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
'https://en.wikipedia.org/w/index.php?title=Uluru</a> '+
'(last visited June 22, 2009).</p>'+
'</div>'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
var marker = new google.maps.Marker({
title: 'Uluru (Ayers Rock)',
map: resultsMap,
position: results[0].geometry.location
});
//Call Listner On Click Markre
marker.addListener('click', function() {
infowindow.open(map, marker);
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>
我使用下面的GoogleAPI来运行
<script async defer src="https://maps.googleapis.com/maps/api/js?key=XXXXXXMYKEYXXXX&callback=initMap"></script>
在这一行上,更改映射以映射变量映射不存在:
geocodeAddress(geocoder, map); // changed "maps" to "map"
当我这样做时,地理代码对我来说工作正常。从我的API中删除&callback=initMap
并添加此代码
$("#myModal").on("shown.bs.modal", function () {
initMap();
google.maps.event.trigger(map, "resize");
});
工作顺利:-
geocodeAddress(geocoder, map); // changed "maps" to "map"
$("#myModal").on("shown.bs.modal", function () {
initMap();
google.maps.event.trigger(map, "resize");
});