1

我正在尝试向我的自定义谷歌地图 API v3 添加一个标记。地图显示正常,但我似乎无法添加标记。这似乎是一件基本的事情,但我一直卡住。我已经尝试按照https://developers.google.com/maps/documentation/javascript/overlays#AddingOverlays上的说明进行操作

但是,我对 javascript 和 API 的一般知识非常有限,所以我觉得这里一定缺少一些基本的东西。太基础了,谷歌的说明可能没有涵盖它。缺少冒号、括号。也许变量的顺序错误?非常感谢任何输入!下面是代码:

<script type="text/javascript">
function initialize() {

    var myLatlng = new google.maps.LatLng(51.60505, -0.18989);

    var mapOptions = {
        center: myLatlng,
        zoom: 18,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };

    var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

    //the code for the marker, as copy pasted from google
    var marker = new google.maps.Marker({
        position: myLatlng,
        title:"Hello World!"
    });

    var styles = [
      {featureType: "landscape",
        stylers: [
          { color: "#378fb5" }
        ]
      },{
       ETC
       ETC
       ETC
        }
   ];

    map.setOptions({styles: styles});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
4

3 回答 3

2

你需要打电话

marker.setMap(map)

创建后,即

<script type="text/javascript">
function initialize() {

var myLatlng = new google.maps.LatLng(51.60505, -0.18989);

var mapOptions = {
    center: myLatlng,
    zoom: 18,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

//the code for the marker, as copy pasted from google
var marker = new google.maps.Marker({
    position: myLatlng,
    title:"Hello World!"
});

// Add the marker to the map
marker.setMap(map);

var styles = [
  {featureType: "landscape",
    stylers: [
      { color: "#378fb5" }
    ]
  },{
   ETC
   ETC
   ETC
    }
];

map.setOptions({styles: styles});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
于 2013-05-29T11:14:39.230 回答
2

您必须像这样精确地标记要用于标记的地图:

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

祝你今天过得愉快 !

于 2013-05-29T11:16:27.647 回答
0

如果要将标记拖放到所需位置,请设置 draggable = true。

var marker = new google.maps.Marker({ map: map, draggable:true });

于 2013-05-29T11:51:25.663 回答