2014-10-26 96 views

回答

2

你有很多方法可以做你想做的事。 奇怪的是,我試圖只使用JavaScript來做到這一點,我使用JSTS庫來計算兩條路徑之間的交集(在我的例子中,幾何圖形是從Bing中檢索的,但我沒有在這個例子中包含請求因爲它沒有幫助)。

使用案例:

所以,你想有兩條路之間(或者你可以使用汽車共享或者你可以和你的朋友,例如運行路線的一部分)的共同路徑如果這是正確的,那麼這個例子會幫助你。

庫:

首先,以下庫是需要:JSTS,您可以通過Github上專用的存儲庫中獲得它:https://github.com/bjornharrtell/jsts

在其他有趣的圖書館是在這裏草坪可供選擇: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上可用的以及所有的工作示例: 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()方法或您以最佳性能操作的結果。

0

我不確定是否完全理解您的請求,但Bing地圖和Google地圖API的路線在響應中包含指定路線值的「距離」字段。

這裏有兩個文件兩個環節:

Bing Maps & Google Maps

有了,你可以比較兩個路徑之間的距離值,找到最長的。

希望得到這個幫助。

+1

這是關於共同的路徑,所以兩條路線相互重疊的路徑。它就像空間數據庫中的STIntersects()。 – 2014-10-28 13:25:40