[英]Google Maps v3 api for localhost not working
我想尝试一个实现Gmaps v3的示例演示,并从Google的文档中尝试了这个示例,但没有输出,页面只加载几秒钟然后空白,没有输出。
<!DOCTYPE html>
<html lang = "en">
<head>
<style type="text/css">
html{height: 100%}
body{height: 100%; margin: 0; padding: 0}
#map-canvas{height: 100%}
</style>
<title>GMaps Demo</title>
<script src = "https://maps.googleapis.com/maps/api/js?
key=${API_KEY}&sensor=false">
</script>
<script>
function initialize(){
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = google.maps.Map(
document.getElementById("map-canvas"),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id = "map-canvas">
</div>
</body>
</html>
首先:谷歌地图3不再需要api密钥,所以你没关系
https://maps.googleapis.com/maps/api/js?sensor=false
作为一个URL。
(正如Jeff Hoye指出的那样,截至2016年6月22日,再次需要API密钥。)
然后你忘了这行中的“新”:
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
通过这些更改,您的地图将会显示出来。
如果没有API密钥,Google地图就无法使用...参考: https : //console.cloud.google.com/apis
这很容易! 在新的API代码中,您应该输入您的引荐来源。 所以对于你的localhost测试:
让它空虚
这里也是google api解释:
Accept requests from these HTTP referrers (web sites) (Optional)
Use asterisks for wildcards.
如果您将此留空,则会从任何推荐人接受请求。
在生产中使用此密钥之前,请务必添加引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.