module Geocoder::Calculations
def random_point_near(center, radius, options = {})
Use Geocoder.configure(:units => ...) to configure default units.
* :units - :mi or :km
The options hash supports:
which returns a [lat,lon] array
* a geocoded object (one which implements a +to_coordinates+ method
* a geocodable address (string)
* an array of coordinates ([lat,lon])
Geocoder methods that accept points as arguments. They can be:
The points are given in the same way that points are given to all
Takes one point, one radius, and an options hash.
around the provided point
Random point within a circle of provided radius centered
#
def random_point_near(center, radius, options = {}) random = Random.new(options[:seed] || Random.new_seed) # convert to coordinate arrays center = extract_coordinates(center) earth_circumference = 2 * Math::PI * earth_radius(options[: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