九月
15

根据两点的经纬度计算两点之间的距离:

# converts degrees to radians
# params: degree to convert
# returns: answer in radians
def self.convert_to_radians(degree_num)
radians = degree_num.to_f * Math::PI/180
end

# calculate the distance between two zips using the great circle ormula
# params: orig latitude, orig longitude, to latitude, to longitude
# returns: distance in miles
def self.calc_distance_between(orig_lat, orig_long, to_lat, to_long)
# find the delta’s of latitude and longitude
delta_lat = to_lat - orig_lat
delta_long = to_long - orig_long

# compute the sin^2(delta_*/2)
computed_lat = Math.sin(delta_lat/2)
computed_lat = computed_lat * computed_lat
computed_long = Math.sin(delta_long/2)
computed_long = computed_long * computed_long

# find the inner part of the great circle
inner_equation = computed_lat + Math.cos(orig_lat) * Math.cos(to_lat) * computed_long
# find the outer part of the great circle
outer_equation = 2 * Math.atan2(Math.sqrt(inner_equation),Math.sqrt(1-inner_equation))
# find the distance in miles
distance = 3956 * outer_equation
end

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg

Tags:

No Responses

Leave a Response

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>