Google地图和Bing地图的方法可以提供地图上从A点到B点的路线。这突出显示了地图上从A到B的路径 - 称为P1 假设P2是从C到D的另一条路径(其他一些点),我们怎样才能找到路径P1和P2之间路径的最长公共长度?
答案 0 :(得分:2)
你有很多方法可以做你想做的事。 奇怪的是,我试图只使用JavaScript来实现这一点,我使用JSTS库来计算两条路线之间的交集(在我的例子中,几何体是从Bing检索的,但我没有在这个例子中包含请求,因为这没有用)。
使用案例
所以,你想拥有两条路径之间的公共路径(或者你可以使用汽车共享的路线的一部分,或者你可以与朋友一起运行的路径),如果这是正确的,那么这个例子将会帮助你。
<强>库:强>
首先,需要以下库:JSTS,您可以通过Github专用存储库获取它:https://github.com/bjornharrtell/jsts
其他有趣的图书馆是Turf:https://github.com/Turfjs/
使用JSTS和传单实施:
以下是在这种情况下有趣的JavaScript片段:
<script type="text/javascript">
var routeCoordinatesA = [[50.619512, 3.061242]....TRUNCATED FOR READIBILITY** ];
var routeCoordinatesB = [[50.619512, 3.061242]....TRUNCATED FOR READIBILITY** ];
$(function () {
var map = L.map('map').setView([47.5, 2.75], 5);
// Add base tile layer - sample from Leaflet website
L.tileLayer('http://{s}.tile.thunderforest.com/transport/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var polylineA = L.polyline(routeCoordinatesA, { color: '#4b98dc' }).addTo(map);
var polylineB = L.polyline(routeCoordinatesB, { color: '#de6262' }).addTo(map);
var geometryFactory = new jsts.geom.GeometryFactory();
// Coordinates adapted to match for jsts
var coordsA = [];
$.each(routeCoordinatesA, function (idx, current) { coordsA.push([current[1], current[0]]); });
var coordsB = [];
$.each(routeCoordinatesB, function (idx, current) { coordsB.push([current[1], current[0]]); });
// Element A
var coordinatesA = bindCoord2JTS(coordsA);
var shellA = geometryFactory.createLinearRing(coordinatesA);
var jstsPolygonA = geometryFactory.createPolygon(shellA);
// Element b
var coordinatesB = bindCoord2JTS(coordsB);
var shellB = geometryFactory.createLinearRing(coordinatesB);
var jstsPolygonB = geometryFactory.createPolygon(shellB);
// Interection
var bufferTolerance = (2 / 1000); // Small buffer to avoid different node no detection
var intersection = shellA.buffer(bufferTolerance).intersection(shellB);
var intersectionPoints = [];
$.each(intersection.getCoordinates(), function (idx, current) {
intersectionPoints.push([current.x, current.y]);
});
intersectionPoints.pop();
var intersectionLine = L.polyline(intersectionPoints, { color: '#4fc281', weight: 8 }).addTo(map);
map.fitBounds(routeCoordinatesA.concat(routeCoordinatesB));
});
var bindCoord2JTS = function (coords) {
var coordinates = [];
for (var i = 0; i < coords.length; i++) {
coordinates.push(new jsts.geom.Coordinate(
coords[i][1], coords[i][0]));
}
return coordinates;
};
您可以在Github上提供的我的Leaflet实验中获取所有工作示例: https://github.com/nicoboo/maps/tree/master
这里是实现我所说的内容的页面: https://github.com/nicoboo/maps/blob/master/Boo.Maps.Web.LeafletExperiments/LeafletWithin/index.html
<强>考虑:强>
当然,这实际上是基于客户端,在服务器端获取信息可能很有用,我建议使用空间启用的数据库,以便您可以使用STBuffer()和STIntersection()直接在列上的方法或您以最佳性能操作的结果。
答案 1 :(得分:0)
我不确定是否完全理解您的请求,但Bing地图和Google地图API都会在其回复中包含&#34;距离&#34;字段,指定方向的值。
这两个文档都有两个链接:
Bing Maps&amp; Google Maps
这样你可以比较两条路径之间的距离值并找到最长的路径。
希望得到这个帮助。