Class: GoogleStaticMapsHelper::Location
- Inherits:
-
Object
- Object
- GoogleStaticMapsHelper::Location
- Defined in:
- lib/google_static_maps_helper/location.rb
Overview
Represents a location with lat and lng values.
This classed is used internally to back up Markers' location and Paths' points.
Defined Under Namespace
Classes: NoLatKey, NoLatMethod, NoLngKey, NoLngMethod
Constant Summary collapse
- EARTH_RADIUS_KM =
:nodoc:
6371- LAT_LNG_PRECISION =
:nodoc:
6
Instance Attribute Summary collapse
-
#lat ⇒ Object
Returns the value of attribute lat.
-
#lng ⇒ Object
Returns the value of attribute lng.
Instance Method Summary collapse
-
#distance_to(location) ⇒ Object
Calculates the distance in meters to given location.
-
#endpoint(distance, heading) ⇒ Object
Returns a new Location which has given distance and heading from current location.
-
#endpoints_for_circle_with_radius(radius, steps = 30) ⇒ Object
Returns ends poionts which will make up a circle around current location and have given radius.
-
#initialize(*args) ⇒ Location
constructor
:call-seq: new(location_object_or_options, *args).
-
#to_url ⇒ Object
Returning the location as a string "lat,lng".
Constructor Details
#initialize(*args) ⇒ Location
:call-seq: new(location_object_or_options, *args)
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.
28 29 30 31 32 33 34 35 36 |
# File 'lib/google_static_maps_helper/location.rb', line 28 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 |
Instance Attribute Details
#lat ⇒ Object
Returns the value of attribute lat.
18 19 20 |
# File 'lib/google_static_maps_helper/location.rb', line 18 def lat @lat end |
#lng ⇒ Object
Returns the value of attribute lng.
18 19 20 |
# File 'lib/google_static_maps_helper/location.rb', line 18 def lng @lng end |
Instance Method Details
#distance_to(location) ⇒ Object
Calculates the distance in meters to given location
- location
Another location which you want the distance to
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/google_static_maps_helper/location.rb', line 43 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) ⇒ Object
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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/google_static_maps_helper/location.rb', line 62 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) ⇒ Object
Returns ends poionts which will make up a circle around current location and have given radius
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/google_static_maps_helper/location.rb', line 81 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 |
#to_url ⇒ Object
Returning the location as a string "lat,lng"
96 97 98 |
# File 'lib/google_static_maps_helper/location.rb', line 96 def to_url # :nodoc: [lat, lng].join(',') end |