【问题标题】:How to create a marker that would be on a polyline, at a certain distance from another marker on the polyline- GoogleMaps API Javascript V3如何在折线上创建一个标记,与折线上的另一个标记有一定距离-GoogleMaps API Javascript V3
【发布时间】:2014-03-22 17:47:25
【问题描述】:

我正在尝试在距离我已经创建的折线的起点一定距离处添加一个标记。 (我有一条带有起点和终点的折线,在这些点上带有标记。我想在距起点的特定距离处添加标记。我尝试使用方法 poly.GetPointsAtDistance(distance) 但显然它不再使用了.我看过不同的帖子和谷歌地图Api,但没有成功。 我有

 var line = new google.maps.Polyline({
 map: map, 
 path: [location1, location2],
 strokeWeight: 7,
 strokeOpacity: 0.8,
 strokeColor: "#FFAA00"
});

我有折线的长度

 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
      });

我希望能够通过给出从起点的距离 (new_distance) 来创建标记。 在类似的东西:

createMarker(map, line.GetPointAtDistance(new_distance), title);

关于使用什么来替换 GetPointAtDistance 的任何建议

【问题讨论】:

  • 只是为了进一步澄清我的问题。我想要的是能够在如下所示的函数中添加标记,例如在 line_length/4 或 line_length/3...等处:createMarker(map, line.GetPointAtDistance(line_length/3), title );

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


【解决方案1】:

函数 .GetPointAtDistance 是 Mike Williams 的epoly library for v2 的一部分。这里有一个移植到 v3 的版本: http://www.geocodezip.com/scripts/v3_epoly.js

example using it on v3

代码:

// === 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);
}

需要google.maps.geometry library

【讨论】:

  • 非常感谢。你介意告诉我如何让插件与我的应用程序一起工作(它是一个 ruby​​ on rails 应用程序),以便我可以使用函数 getPointAtDistance 吗?我是初学者,步骤越详细越好。
  • 它正在工作。非常感谢!我添加了 google.maps.Polyline.prototype.GetPointAtDistance = function(metres) { 它成功了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
  • 2014-02-14
  • 2016-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多