Method: Geocoder::Calculations#random_point_near
- Defined in:
- lib/geocoder/calculations.rb
#random_point_near(center, radius, options = {}) ⇒ Object
Random point within a circle of provided radius centered around the provided point Takes one point, one radius, and an options hash. The points are given in the same way that points are given to all Geocoder methods that accept points as arguments. They can be:
-
an array of coordinates ([lat,lon])
-
a geocodable address (string)
-
a geocoded object (one which implements a
to_coordinates
method which returns a [lat,lon] array
The options hash supports:
-
:units
-:mi
or:km
Use Geocoder.configure(:units => …) to configure default units. -
:seed
- The seed for the random number generator
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/geocoder/calculations.rb', line 238 def random_point_near(center, radius, = {}) random = Random.new([:seed] || Random.new_seed) # convert to coordinate arrays center = extract_coordinates(center) earth_circumference = 2 * Math::PI * earth_radius([:units]) max_degree_delta = 360.0 * (radius / earth_circumference) # random bearing in radians theta = 2 * Math::PI * random.rand # random radius, use the square root to ensure a uniform # distribution of points over the circle r = Math.sqrt(random.rand) * max_degree_delta delta_lat, delta_long = [r * Math.cos(theta), r * Math.sin(theta)] [center[0] + delta_lat, center[1] + delta_long] end |