[英]Why won't google maps show on my website, I have been using the developers website?
/* This is the javascript function that makes the map load */ function initMap() { var mapDiv = document.getElementById('map'); var map = new google.maps.Map(mapDiv, { center: { lat: 44.540, lng: -78.546 }, zoom: 8 }); }
/* This is the basic css */ #map { width: 500px; height: 400px; }
<!-- Javascript called into the html page --> <!DOCTYPE html> <html> <head> </head> <body> <div id="map"></div> <script src="https://maps.googleapis.com/maps/api/js" async defer></script> </body> </html>
您需要添加一個DOM偵聽器,該偵聽器將在窗口加載時執行initMap()
函數。
嘗試這個
function initMap() { var mapDiv = document.getElementById('map'); var map = new google.maps.Map(mapDiv, { center: new google.maps.LatLng(44.540, -78.546), zoom: 8 }); } google.maps.event.addDomListener(window, 'load', initMap);
#map { width: 500px; height: 400px; }
<script src="https://maps.googleapis.com/maps/api/js" ></script> <div id="map"></div>
您的函數未調用請使用$(document).ready(function()())來調用函數
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.11.2.min.js" type="text/javascript"></script> <script src="https://maps.googleapis.com/maps/api/js"></script> <script> $(document).ready(function() { var mapDiv = document.getElementById('map'); var map = new google.maps.Map(mapDiv, { center: {lat: 44.540, lng: -78.546}, zoom: 8 }); }); </script> <style> #map { width: 500px; height: 400px; } </style> </head> <body> <div id="map"></div> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.