Class: ServerSideGoogleMaps::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/server-side-google-maps/point.rb

Constant Summary collapse

RADIUS_OF_EARTH =

metres

6367000
DEGREES_TO_RADIANS =
Math::PI / 180

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(latitude, longitude, options = {}) ⇒ Point

Returns a new instance of Point.



9
10
11
12
13
14
15
# File 'lib/server-side-google-maps/point.rb', line 9

def initialize(latitude, longitude, options = {})
  @latitude = latitude
  @longitude = longitude
  @object = options[:object] if options[:object]
  @distance_along_path = options[:distance_along_path] if options[:distance_along_path]
  @elevation = options[:elevation] if options[:elevation]
end

Instance Attribute Details

#distance_along_pathObject

Returns the value of attribute distance_along_path.



7
8
9
# File 'lib/server-side-google-maps/point.rb', line 7

def distance_along_path
  @distance_along_path
end

#elevationObject (readonly)

Returns the value of attribute elevation.



6
7
8
# File 'lib/server-side-google-maps/point.rb', line 6

def elevation
  @elevation
end

#latitudeObject (readonly)

Returns the value of attribute latitude.



6
7
8
# File 'lib/server-side-google-maps/point.rb', line 6

def latitude
  @latitude
end

#longitudeObject (readonly)

Returns the value of attribute longitude.



6
7
8
# File 'lib/server-side-google-maps/point.rb', line 6

def longitude
  @longitude
end

#objectObject (readonly)

Returns the value of attribute object.



6
7
8
# File 'lib/server-side-google-maps/point.rb', line 6

def object
  @object
end

Instance Method Details

#==(other) ⇒ Object



17
18
19
20
# File 'lib/server-side-google-maps/point.rb', line 17

def ==(other)
  return false unless Point === other
  latitude == other.latitude && longitude == other.longitude && object == other.object
end

#distance(other) ⇒ Object

Calculates the distance to another point, in metres

The method assumes the Earth is a sphere.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/server-side-google-maps/point.rb', line 25

def distance(other)
  lat1 = latitude * DEGREES_TO_RADIANS
  lat2 = other.latitude * DEGREES_TO_RADIANS
  dlat = lat2 - lat1
  dlon = (longitude - other.longitude) * DEGREES_TO_RADIANS

  # Optimize a tad. This method is slow.
  sin_dlat = Math.sin(dlat / 2)
  sin_dlon = Math.sin(dlon / 2)

  a = sin_dlat * sin_dlat + Math.cos(lat1) * Math.cos(lat2) * sin_dlon * sin_dlon

  sqrt_a = Math.sqrt(a)

  c = 2 * Math.asin(1.0 < sqrt_a ? 1.0 : sqrt_a)

  (RADIUS_OF_EARTH * c).to_i
end

#latlng_distance_squared(other) ⇒ Object



44
45
46
47
48
# File 'lib/server-side-google-maps/point.rb', line 44

def latlng_distance_squared(other)
  latitude_difference = latitude - other.latitude
  longitude_difference = longitude - other.longitude
  latitude_difference * latitude_difference + longitude_difference * longitude_difference
end

#latlng_distance_squared_from_segment(segment) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/server-side-google-maps/point.rb', line 50

def latlng_distance_squared_from_segment(segment)
  p1 = segment.first

  vx = p1.latitude - latitude
  vy = p1.longitude - longitude

  return vx*vx + vy*vy if segment.length2 == 0

  ux = segment.dlat
  uy = segment.dlon

  det = (-vx*ux) + (-vy*uy)

  length2 = segment.length2
  if det < 0 || det > length2
    p2 = segment.last
    # We're outside the line segment
    wx = p2.latitude - latitude
    wy = p2.longitude - longitude

    d1 = vx*vx + vy*vy
    d2 = wx*wx + wy*wy
    return d1 < d2 ? d1 : d2
  end

  det = ux*vy - uy*vx

  return det * det / length2
end