如何在Google Maps脚本中插入简单标记?

时间:2015-11-10 16:37:24

标签: javascript html google-maps google-maps-api-3 marker

我有一个Google Maps脚本,看起来像这样(它位于<head></head>标签之间):

<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
    function initialize() {
    var mapCanvas = document.getElementById('map');
    var mapOptions = {
    center: new google.maps.LatLng(51.500891, 5.439642),
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(mapCanvas, mapOptions)
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

以下是<body></body>代码:

<div id="map"></div>

所以我想在这里添加一个红色标记:https://developers.google.com/maps/documentation/javascript/examples/marker-simple

1 个答案:

答案 0 :(得分:0)

从您链接到的示例中获取代码:

var marker = new google.maps.Marker({
    position: myLatLng,
    map: map,
    title: 'Hello World!'
  });

将它添加到地图(并将位置更改为可见)对我有用:

&#13;
&#13;
function initialize() {
  var mapCanvas = document.getElementById('map');
  var mapOptions = {
    center: new google.maps.LatLng(51.500891, 5.439642),
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(mapCanvas, mapOptions);
  var marker = new google.maps.Marker({
    position: mapOptions.center,
    map: map,
    title: 'Hello World!'
  });
}
google.maps.event.addDomListener(window, 'load', initialize);
&#13;
html,
body,
#map {
  height: 100%;
  width: 100%;
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>

<div id="map"></div>
&#13;
&#13;
&#13;