Latitude and Longitude of a Circle in OpenLayers3

0

Good!

I would like to get the Latitude and Longitude of a circle in OpenLayers.

var geometry = evt.feature.getGeometry();
var radius = Math.round(geometry.getRadius() * 100) / 100;
var center = geometry.getCenter();
var lat = ????
var lon = ????

Geometry.getCoordinates () gives me an error "is not a function".

    
asked by Miguel Herreros Cejas 06.11.2017 в 14:19
source

1 answer

0

There is no such method in ol.geom.Circle . Among the possible methods that exist in this class that can help you are: getCenter() , getFisrtCoordinate() , getLastCoordinate()

var layer = new ol.layer.Vector({
  source: new ol.source.Vector()
});


var circle = new ol.geom.Circle([-0.3, 38.5], 10);

var circleFeature = new ol.Feature({
  geometry: circle
});

layer.getSource().addFeature(circleFeature);

// Feature
var center = circleFeature
  // Geometry
  .getGeometry()
  // ol.Coordinate ([x, y])
  .getCenter();
var [lon, lat] = center;
console.log(center, lon, lat);
  
var map = new ol.Map({
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    })
  ],
  target: 'map',
  controls: ol.control.defaults({
    attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
      collapsible: false
    })
  }),
  view: new ol.View({
    center: [0, 0],
    projection: 'EPSG:4326',
    zoom: 2
  })
});

map.addLayer(layer)
#map, html, body {
  width: 100%;
  height: 100%;
  margin:0;
  padding: 0;
}
<link rel="stylesheet" href="https://openlayers.org/en/v4.4.2/css/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.4.2/build/ol.js"></script>
<div id="map"></div>
    
answered by 06.11.2017 / 14:48
source