谷歌地图Api V3:如何将标记绑定到多边形的顶点

Google Maps Api V3: How to bind a marker to the vertices of a Polygon?

本文关键字:绑定 多边形 顶点 V3 Api 谷歌地图      更新时间:2023-09-26

我正在尝试将标记绑定到多边形的顶点,这样移动标记将改变多边形的形状。

    var polygonLine = new google.maps.Polyline(
    {
        path: [],
        map: map,
        strokeColor: "#FF0000",
        strokeOpacity: 1.0,
        strokeWeight: 2
    });
    polygonLine.getPath().push(new google.maps.LatLng(-31.95202, 115.8548));
    polygonLine.getPath().push(new google.maps.LatLng(-31.94980, 115.8586));
    polygonLine.getPath().push(new google.maps.LatLng(-31.95246, 115.8625));
    polygonLine.getPath().push(new google.maps.LatLng(-31.95508, 115.8558));
    var polygon = new google.maps.Polygon({map: map, path: polygonLine.getPath()});
    var vertices = polygon.getPath();
    for (var i = 0; i < vertices.getLength(); i++)
    {
        markers[i] = new google.maps.Marker({ position:vertices.getAt(i), map: map, draggable: true });
        vertices.getAt(i).bindTo('position', markers[i], 'position');      // Throws an error
    }

现在这不起作用,因为在最后一行的第二行,顶点.getAt(i)返回一个LatLng,它不支持"position"属性。

有人知道我如何把记号笔绑在垂直线上吗?感谢:)

我将用v3 API示例来看看这个Google Code项目的源代码。

它似乎基本上添加了一个处理绑定数组的简单类,然后将其与bindTo调用一起使用。