简体   繁体   English

来自API的Google地图未显示

[英]Google map from API not showing

For some reason the google maps isn't showing when I run my file in the browser It just gives a blank space. 由于某种原因,当我在浏览器中运行文件时,谷歌地图没有显示。它只是留有空白。 This is my code: 这是我的代码:

 $(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> 

You have a syntax error in your code (a misplaced ")"). 您的代码中存在语法错误(错误放置的“)”)。 Working code snippet: 工作代码段:

 $(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