【问题标题】:Maps API: Finding the longest common path in two given pathsMaps API:在两个给定路径中查找最长的公共路径
【发布时间】:2014-10-29 00:47:30
【问题描述】:

谷歌地图和必应地图有一些方法可以在地图上给出从 A 点到 B 点的方向。这会在地图上突出显示从 A 到 B 的路径 - 称之为 P1 假设 P2 是从 C 到 D(一些其他点)的另一条路径,我们如何找到路径 P1 和 P2 之间的最长公共路径长度?

【问题讨论】:

    标签: algorithm api google-maps bing-maps


    【解决方案1】:

    你有很多方法可以做你想做的事。 奇怪的是,我尝试仅使用 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: '&copy; <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

    现场演示:http://htmlpreview.github.io/?https://github.com/nicoboo/maps/blob/master/Boo.Maps.Web.LeafletExperiments/LeafletWithin/index.html

    注意事项:

    当然,这实际上是基于客户端的,在服务器端获取信息可能很有用,我建议使用启用空间的数据库,以便您可以使用 STBuffer() 和 STIntersection()直接在列上的方法或您以最佳性能操作的结果。

    【讨论】:

      【解决方案2】:

      我不确定是否完全理解您的请求,但 Bing 地图和用于路线的 Google 地图 API 在其响应中包含指定路线值的“距离”字段。

      以下是两个文档的两个链接:

      Bing Maps & Google Maps

      这样您就可以比较两条路径之间的距离值并找到最长的。

      希望对您有所帮助。

      【讨论】:

      • 是关于公共路径的,所以两条路径相互重叠的路径。这就像空间数据库中的 STIntersects()。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-25
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 2015-06-01
      相关资源
      最近更新 更多