2017-06-15 21 views
0

表示されたマップの距離をマイル/メートルで計算するにはどうすればよいですか?私は地図の中心の座標を持っていると仮定します。私は、中心から地図の左/右の最も表示された部分までの距離/半径を知りたいですか?私はすでにGoogle Mapを使っていますが、Openlayersの新機能です。これを達成する方法はありますか?今のGoogleマップでの私の計算は、OpenLayersをでこれを達成する方法はあります。この表示されているOpenLayersマップの半径を取得

var bounds = this.instance.getBounds(); 

var center = bounds.getCenter(); 
var ne = bounds.getNorthEast(); 

// r = radius of the earth in statute miles 
var r = 3963.0; 
var to_radians_divide = 57.2958; 

// Convert lat or lng from decimal degrees into radians (divide by 57.2958) 
var lat1 = center.lat()/to_radians_divide; 
var lon1 = center.lng()/to_radians_divide; 
var lat2 = ne.lat()/to_radians_divide; 
var lon2 = ne.lng()/to_radians_divide; 

// distance = circle radius from center to Northeast corner of bounds 
var dis = r * Math.acos(Math.sin(lat1) * Math.sin(lat2) + 
     Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)); 

return dis; 

のようなものですか?私はちょうど中心からの表示された地図の左端の部分の距離を見つけたいと思う

答えて

0

これをまたopenlayersで達成することができます。ここにはそのコードがあり、この例ではopenlayers 3.20.0バージョンを使用しています。

var size = map.getSize(); 
    var center = map.getView().getCenter(); 
    var sourceProj = map.getView().getProjection(); 
    var extent = map.getView().calculateExtent(size); 

    extent = ol.proj.transformExtent(extent, sourceProj, 'EPSG:4326'); 
    var posSW = [extent[0], extent[1]]; 
    var posNE = [extent[2], extent[3]]; 
    center = ol.proj.transform(center, sourceProj, 'EPSG:4326'); 

    var wgs84Sphere = new ol.Sphere(6378137); 
    var centerToSW = wgs84Sphere.haversineDistance(center, posSW); 
    var centerToNE = wgs84Sphere.haversineDistance(center, posNE); 
    console.log("centerToSW - ",centerToSW); 
    console.log("centerToNE - ",centerToNE); 
+0

これは私のコードとその作業で十字チェックされているようです。私はおそらくこれを使っています。なぜなら、これは私がやったものよりももっと "Openlayery"だからです。しかし、結果はほぼ同じです。 –

関連する問題