如何在不使用marker = new google.maps.Marker的情况下更改标记选项

时间:2012-03-26 10:51:23

标签: google-maps-api-3 google-maps-markers

我发现下面的函数只创建了一个标记 - 这就是我想要的。 但是如何更改标记选项,例如HTML - 没有创建新的? 即,下面的代码将使用setPosition移动现有标记,但如果我还希望其html和标题更改,该怎么办....

var marker;

function placeMarker(location) {
if ( marker ) {
marker.setPosition(location);
} else {
marker = new google.maps.Marker({
  position: location,
  map: map
});
}
}

3 个答案:

答案 0 :(得分:3)

html是绑定到标记'click'事件的infoWindow的内容。有一个infoWindow.setContent()方法。我会在创建时扩展标记来保存html内容,然后在重置位置,标题等的地方更新它。然后你需要编写自己的'click'事件处理程序来对一个全局的infoWindow使用。< / p>

google.maps.event.addListener(marker, 'click', function() {
                infoWindow.setContent(marker.html);
                infowindow.open(map,marker);
            });

答案 1 :(得分:1)

标记对象的属性大多具有相应的get和set方法as detailed in the documentation

例如,Title有一个get_Title()方法和一个set_Title()方法,您可以像这样使用它...

myMarker.setTitle('my new title');

答案 2 :(得分:1)

Maker是一个MVCObject,这个类有&#34; set&#34;方法

marker.set(property,New_Value);

如果要更改多个属性,可以使用setOptions方法

此致 cadetill