javascript - 在运行时使折线更长

标签 javascript google-maps google-maps-api-3

我有一个谷歌地图应用程序,我可以在多段线上移动标记。我正在生成一个新的目的地点(latlng 坐标)。我想将折线延伸到这个新的目的地点。

有人可以告诉我如何将折线的终点移动到 map 上的另一点。

我在这里使用了polyline[index].getPath(),我可以看到构成路线的所有经纬度坐标对。如何添加到这条路线以使其更长或更短?

最佳答案

简单。如果您只想向现有点数组添加一个附加点,则可以这样做:

var currentPath = polyline[index].getPath();
var newPoint = new google.maps.LatLng(40.781321,-73.965855);
currentPath.push(newPoint);
polyline[index].setPath(currentPath);

如果您想更改当前最后一个点的位置,请尝试以下操作:

var currentPath = polyline[index].getPath();
var newPoint = new google.maps.LatLng(40.781321,-73.965855);
currentPath.setAt(currentPath.getLength()-1, newPoint);
polyline[index].setPath(currentPath);

关于javascript - 在运行时使折线更长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19417935/

相关文章:

javascript - D3.js 中标签的转换 : Rename label names on change and transition smoothly

Javascript发送 "this"对象作为参数

android - 获取当前位置location android

performance - 我可以减少 Google Maps JavaScript API v3 中的请求量吗?

javascript - JS 是否可以在一行中向 html 元素添加一些类并删除一些其他类?

javascript - 如何使用 javascript 模拟鼠标点击(在 Google map 中)?

javascript - Google Map Api 悬停标记放大图标

javascript - 如何在 Google Maps V3 的每条折线段上绘制箭头

javascript - Google Maps API V3 绘图地址

javascript - RequireJS - 无法访问外部模块功能