2014-09-27 124 views

回答

14

可以使用球對象計算像這樣的兩個座標之間的距離:

var distance = ol.sphere.WGS84.haversineDistance([0,0],[180,0]); 
//20037508.34 meters 

球體還提供各種算法來計算像餘弦距離,當量uirectangular等。 您也可以使用不同橢球體的半徑創建球體對象。

我不知道爲什麼文檔是不在線,但你可以檢查可從球體對象的源代碼的方法:我個人認爲,看源代碼是最好的方式https://github.com/openlayers/ol3/blob/master/src/ol/sphere.js

找到關於OpenLayers3的答案;)

+2

功能與世界測地系統(WGS84或epsg4326)的作品。如果需要,使用ol.proj.transform(座標,'EPSG:3857','EPSG:4326')。 – Rafael 2015-01-15 15:02:54

+1

需要注意的是,ol.sphere.WGS84.haversineDistance在ol3產品代碼中不可用(當前爲3.2.0)。事實上該方法沒有被標記爲穩定的,因此它只能在'ol-debug.js'中使用。這也是它不在文檔中的原因。 – htulipe 2015-02-11 08:30:27

+2

[#3222](https://github.com/openlayers/ol3/pull/3222)'ol.Sphere.haversineDistance'現在被標記爲API方法:http://openlayers.org/en/master/apidoc /ol.Sphere.html?unstable=true#haversineDistance另請參閱更新示例:http://openlayers.org/en/master/examples/measure.html – tsauerwein 2015-02-12 08:05:52

3

我正在使用一個相當簡單的解決方案。我實例化兩個點之間的ol.geom.LineString對象並計算線的長度:

 this.formatDistance = function(length) { 
      if (length >= 1000) { 
       length = (Math.round(length/1000 * 100)/100) + 
       ' ' + 'km'; 
      } else { 
       length = Math.round(length) + 
       ' ' + 'm'; 
      } 
      return length; 
     } 

編輯:

 this.distanceBetweenPoints = function(latlng1, latlng2){ 
      var line = new ol.geom.LineString([latlng1, latlng2]); 
      return Math.round(line.getLength() * 100)/100; 
     }; 

然後你可以使用一些格式化得到一個可讀的值新計算方法

實際上,您使用的投影距離可能是錯誤的。 我們有這個上OL3的github上一個相當長時間的討論,你可以在那裏看到它: https://github.com/openlayers/ol3/issues/3533

總之,你需要使用的功能,以獲得精確的演算:

/** 
* format length output 
* @param {ol.geom.LineString} line 
* @return {string} 
*/ 
export default function mapFormatLength(projection, line) { 
    var length; 
    var coordinates = line.getCoordinates(); 
    length = 0; 
    for (var i = 0, ii = coordinates.length - 1; i < ii; ++i) { 
    var c1 = ol.proj.transform(coordinates[i], projection, 'EPSG:4326'); 
    var c2 = ol.proj.transform(coordinates[i + 1], projection, 'EPSG:4326'); 
    length += mapConst.wgs84Sphere.haversineDistance(c1, c2); 
    } 
    var output; 
    if (length > 1000) { 
    output = (Math.round(length/1000 * 100)/100) + 
    ' ' + 'km'; 
    } else { 
    output = (Math.round(length * 100)/100) + 
    ' ' + 'm'; 
    } 
    return output; 
} 
+0

有沒有辦法避免使用mapConst.wgs84Sphere.haversineDistance?它在生產版本中不可用。 – Phil 2016-05-16 18:15:01

+0

我想在新版本的開發者中,他們引入了一種方法來獲得正確的長度在api – 2016-05-17 14:06:16

2

只是爲了再添加一個選項。這不依賴於ol3。

function toRad(x) {return x * Math.PI/180;} 

function SphericalCosinus(lat1, lon1, lat2, lon2) { 

    var R = 6371; // km 
    var dLon = toRad(lon2 - lon1), 
     lat1 = toRad(lat1), 
     lat2 = toRad(lat2), 
     d = Math.acos(Math.sin(lat1)*Math.sin(lat2) + Math.cos(lat1)*Math.cos(lat2) * Math.cos(dLon)) * R; 


    return d; 
} 
+0

我剛剛遇到這個,它似乎很好,很簡單,但我得到的結果似乎也是對於我測量的距離來說是高的。我不想提出一個新問題,但想知道你是否可以幫助我調試它@JonatasWalker? – dvmac01 2016-08-03 11:14:43

+0

在您的服務@ dvmac01。你使用'EPSG:4326'嗎? – 2016-08-03 20:46:17

+0

直到你提到它;)這似乎已經做到了。它仍然會返回比其他地方(Google地圖/維基百科)報告的測量結果高一些的測量結果,但是如果有的話它可能太精確了?一位同事指引我寫這篇文章:https://en.wikipedia.org/wiki/Coastline_paradox,它解釋了這個問題,所以我正在考慮某種平滑方法,不知道你是否可以提出一些建議,但感謝提示! – dvmac01 2016-08-04 08:57:33

0

我寫了這一個我自己,我跳這將是有用的,它返回麥特斯距離:

function getCoordsDistance(firstPoint, secondPoint, projection) { 
    projection = projection || 'EPSG:4326'; 

    length = 0; 
    var sourceProj = mapObj.getView().getProjection(); 
    var c1 = ol.proj.transform(firstPoint, sourceProj, projection); 
    var c2 = ol.proj.transform(secondPoint, sourceProj, projection); 

    var wgs84Sphere = new ol.Sphere(6378137); 
    length += wgs84Sphere.haversineDistance(c1, c2); 

    return length; 
}