获取显示的OpenLayers地图的半径

时间:2017-06-15 19:39:34

标签: javascript openlayers openlayers-3

如何计算显示地图的英里/米距离?假设我有地图中心的坐标。我想知道从地图中心到左/右显示部分的距离/半径?我已经使用谷歌地图获得了这个,但我是Openlayers的新手。有没有办法实现这个目标?现在我在GoogleMap中的计算就像这样

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;

有没有办法在OpenLayers中实现这一目标?我只想找出所显示地图最左边部分与中心的距离

1 个答案:

答案 0 :(得分: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);