diff --git a/examples/tissot.js b/examples/tissot.js index 909df77ae3..3fcb7afebd 100644 --- a/examples/tissot.js +++ b/examples/tissot.js @@ -1,6 +1,7 @@ goog.require('ol.Feature'); goog.require('ol.Map'); goog.require('ol.View2D'); +goog.require('ol.geom.Polygon'); goog.require('ol.layer.Tile'); goog.require('ol.layer.Vector'); goog.require('ol.source.TileWMS'); @@ -38,7 +39,7 @@ var map = new ol.Map({ var radius = 800000; for (var x = -180; x < 180; x += 30) { for (var y = -90; y < 90; y += 30) { - var geometry = ol.sphere.WGS84.circle([x, y], radius, 64); - vectorSource.addFeature(new ol.Feature(geometry)); + var circle = ol.geom.Polygon.circular(ol.sphere.WGS84, [x, y], radius, 64); + vectorSource.addFeature(new ol.Feature(circle)); } } diff --git a/src/ol/geolocation.js b/src/ol/geolocation.js index bc7fe3a501..7a6000bae6 100644 --- a/src/ol/geolocation.js +++ b/src/ol/geolocation.js @@ -11,6 +11,7 @@ goog.require('ol.BrowserFeature'); goog.require('ol.Coordinate'); goog.require('ol.Object'); goog.require('ol.geom.Geometry'); +goog.require('ol.geom.Polygon'); goog.require('ol.proj'); goog.require('ol.sphere.WGS84'); @@ -186,7 +187,8 @@ ol.Geolocation.prototype.positionChange_ = function(position) { this.set(ol.GeolocationProperty.POSITION, projectedPosition); this.set(ol.GeolocationProperty.SPEED, goog.isNull(coords.speed) ? undefined : coords.speed); - var geometry = ol.sphere.WGS84.circle(this.position_, coords.accuracy); + var geometry = ol.geom.Polygon.circular( + ol.sphere.WGS84, this.position_, coords.accuracy); geometry.applyTransform(this.transform_); this.set(ol.GeolocationProperty.ACCURACY_GEOMETRY, geometry); this.dispatchChangeEvent(); diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index 0d7bfd605e..b7a484410a 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -314,3 +314,29 @@ ol.geom.Polygon.prototype.setFlatCoordinates = this.ends_ = ends; this.dispatchChangeEvent(); }; + + +/** + * Create an approximation of a circle on the surface of a sphere. + * @param {ol.Sphere} sphere The sphere. + * @param {ol.Coordinate} center Center. + * @param {number} radius Radius. + * @param {number=} opt_n Optional number of points. Default is `32`. + * @return {ol.geom.Polygon} Circle geometry. + * @todo api + */ +ol.geom.Polygon.circular = function(sphere, center, radius, opt_n) { + var n = goog.isDef(opt_n) ? opt_n : 32; + /** @type {Array.} */ + var flatCoordinates = []; + var i; + for (i = 0; i < n; ++i) { + goog.array.extend( + flatCoordinates, sphere.offset(center, radius, 2 * Math.PI * i / n)); + } + flatCoordinates.push(flatCoordinates[0], flatCoordinates[1]); + var polygon = new ol.geom.Polygon(null); + polygon.setFlatCoordinates( + ol.geom.GeometryLayout.XY, flatCoordinates, [flatCoordinates.length]); + return polygon; +}; diff --git a/src/ol/sphere/sphere.js b/src/ol/sphere/sphere.js index ed095e7736..37ccca8d38 100644 --- a/src/ol/sphere/sphere.js +++ b/src/ol/sphere/sphere.js @@ -12,7 +12,6 @@ goog.provide('ol.Sphere'); goog.require('goog.array'); goog.require('goog.math'); -goog.require('ol.geom.Polygon'); @@ -30,33 +29,6 @@ ol.Sphere = function(radius) { }; -/** - * Returns an approximation to a circle centered on `center` with radius - * `radius` with `n` distinct points. - * - * @param {ol.Coordinate} center Center. - * @param {number} radius Radius. - * @param {number=} opt_n N. - * @return {ol.geom.Geometry} Circle geometry. - * @todo api - */ -ol.Sphere.prototype.circle = function(center, radius, opt_n) { - var n = goog.isDef(opt_n) ? opt_n : 32; - /** @type {Array.} */ - var flatCoordinates = []; - var i; - for (i = 0; i < n; ++i) { - goog.array.extend( - flatCoordinates, this.offset(center, radius, 2 * Math.PI * i / n)); - } - flatCoordinates.push(flatCoordinates[0], flatCoordinates[1]); - var polygon = new ol.geom.Polygon(null); - polygon.setFlatCoordinates( - ol.geom.GeometryLayout.XY, flatCoordinates, [flatCoordinates.length]); - return polygon; -}; - - /** * Returns the distance from c1 to c2 using the spherical law of cosines. *