class GoogleStaticMapsHelper::Location
Represents a location with lat and lng values.
This classed is used internally to back up Markers' location and Paths' points.
Attributes
lat[RW]
lng[RW]
Public Class Methods
new(location_object_or_options, *args)
click to toggle source
Creates a new Location
which is used by Marker
and Path
object to represent it's locations.
:args
: Either a location which responds to lat or lng, or a Hash which has :lat and :lng keys.
# File lib/google_static_maps_helper/location.rb, line 27 def initialize(*args) raise ArgumentError, "Must have some arguments." if args.length == 0 if args.first.is_a? Hash extract_location_from_hash!(args.first) else extract_location_from_object(args.shift) end end
Public Instance Methods
distance_to(location)
click to toggle source
Calculates the distance in meters to given location
location
-
Another location which you want the distance to
# File lib/google_static_maps_helper/location.rb, line 42 def distance_to(location) dLat = deg2rad(location.lat - lat) dLon = deg2rad((location.lng - lng).abs) dPhi = Math.log(Math.tan(deg2rad(location.lat) / 2 + Math::PI / 4) / Math.tan(deg2rad(lat) / 2 + Math::PI / 4)); q = (dLat.abs > 1e-10) ? dLat/dPhi : Math.cos(deg2rad(lat)); dLon = 2 * Math::PI - dLon if (dLon > Math::PI) d = Math.sqrt(dLat * dLat + q * q * dLon * dLon); (d * EARTH_RADIUS_KM * 1000).round end
endpoint(distance, heading)
click to toggle source
Returns a new Location
which has given distance and heading from current location
distance
-
The distance in meters for the new
Location
from current heading
-
The heading in degrees we should go from current
# File lib/google_static_maps_helper/location.rb, line 61 def endpoint(distance, heading) d = (distance / 1000.0) / EARTH_RADIUS_KM; heading = deg2rad(heading); oX = lng * Math::PI / 180; oY = lat * Math::PI / 180; y = Math.asin(Math.sin(oY) * Math.cos(d) + Math.cos(oY) * Math.sin(d) * Math.cos(heading)); x = oX + Math.atan2(Math.sin(heading) * Math.sin(d) * Math.cos(oY), Math.cos(d) - Math.sin(oY) * Math.sin(y)); y = y * 180 / Math::PI; x = x * 180 / Math::PI; self.class.new(:lat => y, :lng => x) end
endpoints_for_circle_with_radius(radius, steps = 30)
click to toggle source
Returns ends poionts which will make up a circle around current location and have given radius
# File lib/google_static_maps_helper/location.rb, line 80 def endpoints_for_circle_with_radius(radius, steps = 30) raise ArgumentError, "Number of points has to be in range of 1..360!" unless (1..360).include? steps points = [] steps.times do |i| points << endpoint(radius, i * 360 / steps) end points << points.first points end
Private Instance Methods
deg2rad(deg)
click to toggle source
# File lib/google_static_maps_helper/location.rb, line 127 def deg2rad(deg) deg * Math::PI / 180; end
extract_location_from_hash!(location_hash)
click to toggle source
# File lib/google_static_maps_helper/location.rb, line 107 def extract_location_from_hash!(location_hash) raise NoLngKey unless location_hash.has_key? :lng raise NoLatKey unless location_hash.has_key? :lat self.lat = location_hash.delete(:lat) self.lng = location_hash.delete(:lng) end
extract_location_from_object(location)
click to toggle source
# File lib/google_static_maps_helper/location.rb, line 114 def extract_location_from_object(location) raise NoLngMethod unless location.respond_to? :lng raise NoLatMethod unless location.respond_to? :lat self.lat = location.lat self.lng = location.lng end
lng_lat_to_precision(number, precision)
click to toggle source
# File lib/google_static_maps_helper/location.rb, line 121 def lng_lat_to_precision(number, precision) rounded = (Float(number) * 10**precision).round.to_f / 10**precision rounded = rounded.to_i if rounded.to_i == rounded rounded end