-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3e5749
commit 08a9143
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,18 @@ | ||
class Location < ActiveRecord::Base | ||
def self.distance loc1, loc2 | ||
rad_per_deg = Math::PI/180 # PI / 180 | ||
rkm = 6371 # Earth radius in kilometers | ||
rm = rkm * 1000 # Radius in meters | ||
|
||
dlat_rad = (loc2.latitude-loc1.latitude) * rad_per_deg # Delta, converted to rad | ||
dlon_rad = (loc2.longitude-loc1.longitude) * rad_per_deg | ||
|
||
lat1_rad, lon1_rad = [loc1.latitude, loc1.longitude].map {|i| i * rad_per_deg } | ||
lat2_rad, lon2_rad = [loc2.latitude, loc2.longitude] .map {|i| i * rad_per_deg } | ||
|
||
a = Math.sin(dlat_rad/2)**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * Math.sin(dlon_rad/2)**2 | ||
c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a)) | ||
|
||
rm * c # Delta in meters | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
class Trip < ActiveRecord::Base | ||
belongs_to :order | ||
belongs_to :user | ||
|
||
def distance(trip) | ||
start_loc1 = Location.find(start_location_id) | ||
end_loc1 = Location.find(end_location_id) | ||
|
||
start_loc2 = Location.find(trip.start_location_id) | ||
end_loc2 = Location.find(trip.end_location_id) | ||
|
||
Location.distance(start_loc1, start_loc2) + Location.distance(end_loc1, end_loc2) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,8 @@ | |
member do | ||
get 'deliver' | ||
end | ||
collection do | ||
post 'search' | ||
end | ||
end | ||
end |