问题描述
我试图在已经创建的折线上的起始点的某个距离处添加一个标记。
(我有一个多段线,起点和终点在这些点上有标记。我想在距离起始点特定距离处添加标记。我尝试使用方法poly.GetPointsAtDistance(distance),但显然它是我已经看过不同的帖子和谷歌地图Api,但没有成功使它的工作。
我有
I am trying to add a marker at a certain distance form the starting point on the polyline I already created.(I have a polyline with start point and end point with markers at those points.I want to add markers at specific distances from the start point. I tried to use the method poly.GetPointsAtDistance(distance) but apparently it is not in use anymore. I have looked at different posts and google Maps Api but haven't succeeded to make it work.I have
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());)
我有一个函数来创建标记:
I have a function to create the marker:
function createMarker(map, latlng, title){
var marker = new google.maps.Marker({
position:latlng,
map:map,
title: title
});
我希望能够通过给出距离(new_distance)从起点创建标记。
类似于:
I would like to be able to create the marker by giving the distance (new_distance) From start point.In something like:
createMarker(map, line.GetPointAtDistance(new_distance), title);
任何有关如何取代GetPointAtDistance的建议
Any suggestion to what to use to replace GetPointAtDistance
推荐答案
函数.GetPointAtDistance是Mike Williams的。这里有一个版本移植到v3:
The function .GetPointAtDistance is part of Mike Williams' epoly library for v2. There is a version ported to v3 here:http://www.geocodezip.com/scripts/v3_epoly.js
代码:
// === 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);
}
需要。
这篇关于如何创建多段线上的标记,与多段线上的另一个标记相距一定距离 - GoogleMaps API Javascript V3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!