如何创建位于多段线上的标记,与多段线上的另一个标记保持一定距离-GoogleMaps API Javascript V3
我正在尝试在距离已创建的多段线上的起点一定距离处添加标记。 (我有一条带起点和终点的多段线,在这些点上有标记。我想在距起点的特定距离处添加标记。我尝试使用poly.GetPointsAtDistance(distance)方法,但显然它不再使用。我查看了不同的帖子和google Maps Api,但没有成功地使其起作用。 我有 我有多段线的长度如何创建位于多段线上的标记,与多段线上的另一个标记保持一定距离-GoogleMaps API Javascript V3,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我正在尝试在距离已创建的多段线上的起点一定距离处添加标记。 (我有一条带起点和终点的多段线,在这些点上有标记。我想在距起点的特定距离处添加标记。我尝试使用poly.GetPointsAtDistance(distance)方法,但显然它不再使用。我查看了不同的帖子和google Maps Api,但没有成功地使其起作用。 我有 我有多段线的长度 var line_length = google.maps.geometry.spherical.computeLength(line.getPath
var line_length = google.maps.geometry.spherical.computeLength(line.getPath());)
我有一个创建标记的函数:
function createMarker(map, latlng, title){
var marker = new google.maps.Marker({
position:latlng,
map:map,
title: title
});
我希望能够通过给出距起点的距离(新距离)来创建标记。
例如:
createMarker(map, line.GetPointAtDistance(new_distance), title);
关于使用什么替换函数GetPointAtDistance的任何建议。GetPointAtDistance是Mike Williams的一部分。这里有一个移植到v3的版本: 代码:
//==返回沿路径指定距离的点的google.maps.LatLng的方法===
//==如果路径短于指定距离,则返回null===
google.maps.Polyline.prototype.GetPointAtDistance=函数(米){
//一些棘手的特殊情况
if(meters==0)返回此.getPath().getAt(0);
如果(米<0)返回空值;
if(this.getPath().getLength()<2)返回null;
var-dist=0;
var-olddist=0;
对于(var i=1;(i
需要。只是为了进一步澄清我的问题。我希望能够在函数中添加一个标记,例如在line_length/4或line_length/3…等,如下所示:createMarker(map,line.GetPointAtDistance(line_length/3),title);非常感谢。您介意告诉我如何让插件与我的应用程序一起工作吗(这是一个ruby on rails应用程序)所以我可以使用函数getPointAtDistance?我是一个初学者,所以步骤越详细越好。它正在工作。非常感谢!我添加了google.maps.Polyline.prototype.getPointAtDistance=函数(米){并且它成功了
createMarker(map, line.GetPointAtDistance(new_distance), title);
// === A method which returns a google.maps.LatLng of a point a given distance along the path ===
// === Returns null if the path is shorter than the specified distance ===
google.maps.Polyline.prototype.GetPointAtDistance = function(metres) {
// some awkward special cases
if (metres == 0) return this.getPath().getAt(0);
if (metres < 0) return null;
if (this.getPath().getLength() < 2) return null;
var dist=0;
var olddist=0;
for (var i=1; (i < this.getPath().getLength() && dist < metres); i++) {
olddist = dist;
dist += google.maps.geometry.spherical.computeDistanceBetween(
this.getPath().getAt(i),
this.getPath().getAt(i-1)
);
}
if (dist < metres) {
return null;
}
var p1= this.getPath().getAt(i-2);
var p2= this.getPath().getAt(i-1);
var m = (metres-olddist)/(dist-olddist);
return new google.maps.LatLng( p1.lat() + (p2.lat()-p1.lat())*m, p1.lng() + (p2.lng()-p1.lng())*m);
}