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-4.0.0 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.5.0 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.4.2 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.4.1 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.4.0 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.3.7 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.3.6 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.3.5 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.3.4 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.3.3 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.3.2 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.3.1 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.3.0 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.2.0 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.1.1 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.1.0 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.0.8 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.0.7 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.0.6 lib/cocoa/sugarcube-corelocation/core_location.rb
sugarcube-3.0.5 lib/cocoa/sugarcube-corelocation/core_location.rb