Sha256: 8a60565986a5cf5dd7621037975b8bfecb2d69241ebc4224ed0980cf57601190

Contents?: true

Size: 1.24 KB

Versions: 88

Compression:

Stored size: 1.24 KB

Contents

# denver = CLLocationCoordinate2D.new(39.764032,-104.963112)
# cincinnati = CLLocationCoordinate2D.new(39.267024,-84.251736)
# denver.distance_to(cincinnati).in_miles
# denver.delta_miles(1101.6, -32.556)  # go east 1,101.6 miles, and south 32.556 miles
class CLLocationCoordinate2D

  def delta_miles(dx, dy)
    earth_radius = 3963.19
    _sugarcube_delta(dx, dy, earth_radius)
  end

  def delta_kilometers(dx, dy)
    earth_radius = 6378.137
    _sugarcube_delta(dx, dy, earth_radius)
  end

  def _sugarcube_delta(dx, dy, earth_radius)
    radius_at_latitude = earth_radius * Math.cos(self.latitude * Math::PI / 180.0)
    if radius_at_latitude > 0
      delta_lng = dx / radius_at_latitude
      delta_lng *= 180.0 / Math::PI
    else
      delta_lng = 0
    end

    delta_lat = dy / earth_radius
    delta_lat *= 180.0 / Math::PI
    CLLocationCoordinate2D.new(self.latitude + delta_lat, self.longitude + delta_lng)
  end
  private :_sugarcube_delta

  def distance_to(cl_location_2d)
    my_location = CLLocation.alloc.initWithLatitude(self.latitude, longitude:self.longitude)
    other_location = CLLocation.alloc.initWithLatitude(cl_location_2d.latitude, longitude:cl_location_2d.longitude)
    my_location.distanceFromLocation(other_location)
  end

end

Version data entries

88 entries across 88 versions & 1 rubygems

Version Path
sugarcube-1.3.7 lib/sugarcube-corelocation/core_location.rb
sugarcube-1.3.6 lib/sugarcube-corelocation/core_location.rb
sugarcube-1.3.5 lib/sugarcube-corelocation/core_location.rb
sugarcube-1.3.4 lib/sugarcube-corelocation/core_location.rb
sugarcube-1.3.3 lib/sugarcube-corelocation/core_location.rb
sugarcube-1.3.2 lib/sugarcube-corelocation/core_location.rb
sugarcube-1.3.1 lib/sugarcube-corelocation/core_location.rb
sugarcube-1.3.0 lib/sugarcube-corelocation/core_location.rb