簡體   English   中英

來自API的Google地圖未顯示

[英]Google map from API not showing

由於某種原因,當我在瀏覽器中運行文件時,谷歌地圖沒有顯示。它只是留有空白。 這是我的代碼:

 $(document).ready(function() { //google map var map, geocoder; function initializeMap() { google.maps.event.addDomListener(window, 'load', function() { codeAddress('33 W 23rd St, New York, NY'); }); } function codeAddress(address) { geocoder = new google.maps.Geocoder(); geocoder.geocode({ 'address': address }, function(results, status) { if (status === google.maps.GeocoderStatus.OK) { var mapOptions = { zoom: 8, center: results[0].geometry.location, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map($("#map").get(0), mapOptions); var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location, }); } else { alert('Geocode was not successful for the following reason: ' + status); } ) }; } initializeMap(); }); 
 #map { height: 300px; width: 800px; border: 1px solid black; } 
 <html> <head> <title>Title</title> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> <script src="test.js"></script> </head> <body> <div id="map"></div> </body> </html> 

您的代碼中存在語法錯誤(錯誤放置的“)”)。 工作代碼段:

 $(document).ready(function() { //google map var map, geocoder; function initializeMap() { google.maps.event.addDomListener(window, 'load', function() { codeAddress('33 W 23rd St, New York, NY'); }); } function codeAddress(address) { geocoder = new google.maps.Geocoder(); geocoder.geocode({ 'address': address }, function(results, status) { if (status === google.maps.GeocoderStatus.OK) { var mapOptions = { zoom: 8, center: results[0].geometry.location, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map($("#map").get(0), mapOptions); var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location, }); } else { alert('Geocode was not successful for the following reason: ' + status); } }); } initializeMap(); }); 
 #map { height: 300px; width: 800px; border: 1px solid black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> <div id="map"></div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM