From 91ef572ad71069807d1a3ab24ad5ed2b45469c0a Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Wed, 7 Dec 2016 10:07:33 +1100 Subject: [PATCH] work around circular dependency by using LngLatBoundsLike --- debug/lng-lat-tobounds.html | 59 +++++++++++++++++++++++++++++++++++++ js/geo/lng_lat.js | 15 ++++------ test/js/geo/lng_lat.test.js | 3 +- 3 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 debug/lng-lat-tobounds.html diff --git a/debug/lng-lat-tobounds.html b/debug/lng-lat-tobounds.html new file mode 100644 index 00000000000..ab6dc1986ca --- /dev/null +++ b/debug/lng-lat-tobounds.html @@ -0,0 +1,59 @@ + + + + Mapbox GL JS debug page + + + + + + + +
+ + + + + + + diff --git a/js/geo/lng_lat.js b/js/geo/lng_lat.js index c4727bc1815..88a258d094e 100644 --- a/js/geo/lng_lat.js +++ b/js/geo/lng_lat.js @@ -1,7 +1,6 @@ 'use strict'; const wrap = require('../util/util').wrap; -const LngLatBounds = require('./lng_lat_bounds'); /** * A `LngLat` object represents a given longitude and latitude coordinate, measured in degrees. @@ -71,23 +70,21 @@ class LngLat { } /** - * Returns a `LngLatBounds` from the coordinates extended by a given `radius`. + * Returns a `LngLatBoundsLike` from the coordinates extended by a given `radius`. * * @param {number} radius Distance in meters from the coordinates to extend the bounds. - * @returns {LngLatBounds} A new `LngLatBounds` object. + * @returns {LngLatBoundsLike} A new `LngLatBoundsLike` object representing the coordinates extended by the `radius`. * @example * var ll = new mapboxgl.LngLat(-73.9749, 40.7736); - * ll.toBounds(100).toString(); // = "LngLatBounds(LngLat(XXX), LngLat(XXX))" + * ll.toBounds(100); // = [[-73.97501862141328, 40.77351016847229], [-73.97478137858673, 40.77368983152771]] */ toBounds(radius) { const latAccuracy = 360 * radius / 40075017, lngAccuracy = latAccuracy / Math.cos((Math.PI / 180) * this.lat); - //return [ [this.lng - lngAccuracy, this.lat - latAccuracy], - // [this.lng + lngAccuracy, this.lat + latAccuracy] ]; - return new LngLatBounds( - new LngLat(this.lng - lngAccuracy, this.lat - latAccuracy), - new LngLat(this.lng + lngAccuracy, this.lat + latAccuracy)); + // returning a LngLatBounds object is troublesome due to the circular dependency hence a simple LngLatBoundsLike object is used + return [ [this.lng - lngAccuracy, this.lat - latAccuracy], + [this.lng + lngAccuracy, this.lat + latAccuracy] ]; } } diff --git a/test/js/geo/lng_lat.test.js b/test/js/geo/lng_lat.test.js index 9e4424f0993..3fd4808e0dc 100644 --- a/test/js/geo/lng_lat.test.js +++ b/test/js/geo/lng_lat.test.js @@ -51,7 +51,8 @@ test('LngLat', (t) => { }); t.test('#toBounds', (t) => { - t.deepEqual(new LngLat(0, 0).toBounds(10), { _ne: {lng: 0.000089832, lat: 0.000089832}, _sw: {lng: -0.000089832, lat: -0.000089832}}); + t.deepEqual(new LngLat(0, 0).toBounds(10), [[-0.00008983152770714982, -0.00008983152770714982], [0.00008983152770714982, 0.00008983152770714982]]); + t.deepEqual(new LngLat(-73.9749, 40.7736).toBounds(10), [[-73.97501862141328, 40.77351016847229], [-73.97478137858673, 40.77368983152771]]); t.end(); });