如何在openlayers中绘制半径为半径的圆 [英] how to draw circle with radius in openlayers

查看:2208
本文介绍了如何在openlayers中绘制半径为半径的圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在openlayers中绘制一个以km为单位的特定半径的圆.与特定地理位置的中心?我跟 http://demo.gwt一样-openlayers.org/gwt_ol_showcase/GwtOpenLayersShowcase.html?example=Draw%20Regular%20Polygon%20Example ,但我想使用预定义的值自动绘制.

I want to draw a circle in openlayers with specific radius in km. with the center of specific geo point? I follow same as http://demo.gwt-openlayers.org/gwt_ol_showcase/GwtOpenLayersShowcase.html?example=Draw%20Regular%20Polygon%20Example but I want to draw automatically with predefined values.

推荐答案

以下代码将为您提供浅蓝色的圆圈,该圆圈以当前地图的中心为中心,半径以米为单位.如果您希望以英尺为单位,只需更换:

The following code will give you a circle in light blue centered at the center of the current map with a radius defined in meters. If you want it in feet, just replace:

        var radius = (radius / ol.proj.METERS_PER_UNIT.m) * resolutionFactor;

具有:

        var radius = (radius / ol.proj.METERS_PER_UNIT.ft) * resolutionFactor;

代码:

   var drawCircleInMeter = function(map, radius) {
        var view = map.getView();
        var projection = view.getProjection();
        var resolutionAtEquator = view.getResolution();
        var center = map.getView().getCenter();
        var pointResolution = projection.getPointResolution(resolutionAtEquator, center);
        var resolutionFactor = resolutionAtEquator/pointResolution;
        var radius = (radius / ol.proj.METERS_PER_UNIT.m) * resolutionFactor;


        var circle = new ol.geom.Circle(center, radius);
        var circleFeature = new ol.Feature(circle);

        // Source and vector layer
        var vectorSource = new ol.source.Vector({
            projection: 'EPSG:4326'
        });
        vectorSource.addFeature(circleFeature);
        var vectorLayer = new ol.layer.Vector({
            source: vectorSource
        });

        map.addLayer(vectorLayer);
    }

NB.此处创建的OpenLayers圆始终是完美平坦的2d圆.而实际上,您可能需要一些可以更好地考虑地图投影(尤其是靠近两极)的东西.

NB. The OpenLayers circle created here is always a perfectly flat 2d circle. Whereas in reality you may want something that takes better account of the map projection (especially close to the poles).

这篇关于如何在openlayers中绘制半径为半径的圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆