我正在尝试使用 Google Maps API 设置一个简单的页面来显示位置上的标记。但是我没有得到这个代码的标记,它基于谷歌的示例代码 here .我改变了来源
"https://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=false"
到
"http://maps.google.com/maps/api/js?sensor=false"
因为被推荐了here 为了避免使用 API key 。 var 标记声明是从谷歌复制的 here .必须更改什么才能显示我的标记?
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map-canvas { height: 100% }
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
</script>
<script type="text/javascript">
function initialize() {
var location = new google.maps.LatLng(59.272, 10.418);
var mapOptions = {
center: location,
zoom: 8
};
var marker = new google.maps.Marker({
position: location,
map: map,
title: 'Hello World!'
});
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"/>
</body>
</html>
最佳答案
将设置 map 的行移动到设置标记的行之前,因为标记在其调用中使用 map :
function initialize() {
var location = new google.maps.LatLng(59.272, 10.418);
var mapOptions = {
center: location,
zoom: 8
};
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
var marker = new google.maps.Marker({
position: location,
map: map,
title: 'Hello World!'
});
}
关于javascript - 使用 Google Maps API 显示标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21306643/