简体   繁体   English

获取显示的OpenLayers地图的半径

[英]Get Radius of the displayed OpenLayers Map

How do I compute the distance in mile/meters of the displayed Map? 如何计算显示地图的距离(以英里/米为单位)? Assuming I have the coordinates of the center of the map. 假设我有地图中心的坐标。 I would like to know the distance/radius from the center to the left/right most displayed part of the map? 我想知道从中心到地图最左/最右部分的距离/半径吗? I already got this using Google Map but I'm new in Openlayers. 我已经使用Google Map找到了这个,但是我是Openlayers的新手。 Is there a way to achieve this? 有没有办法做到这一点? Right now my computation in GoogleMap is like this 现在我在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;

Is there a way to achieve this in OpenLayers? 有没有办法在OpenLayers中实现这一目标? I just want to find out the distance of the left most part of the displayed map from the center 我只想找出显示的地图最左边部分离中心的距离

You can achieve this in openlayers also. 您也可以在openlayers中实现。 Here is the code for that and I am using openlayers 3.20.0 version for this example. 这是代码,我在此示例中使用的是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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM