Skip to content
Snippets Groups Projects
Commit 79a829c5 authored by Alex Reisner's avatar Alex Reisner
Browse files

Make bearing calculation Postgres-compatible.

Only worked with MySQL.
parent 9eab2859
No related branches found
No related tags found
No related merge requests found
......@@ -88,17 +88,16 @@ module Geocoder::Orm
bearing = "(DEGREES(ATAN2( " +
"SIN(RADIANS(#{lon_attr} - #{longitude})) * " +
"COS(RADIANS(#{lat_attr})), (" +
"COS(RADIANS(#{latitude})) * " +
"SIN(RADIANS(#{lat_attr}))) - " +
"(SIN(RADIANS(#{latitude})) * " +
"COS(RADIANS(#{lat_attr})) * " +
"COS(RADIANS(#{lon_attr} - #{longitude}))))) + 360) % 360"
"COS(RADIANS(#{latitude})) * SIN(RADIANS(#{lat_attr}))" +
") - (" +
"SIN(RADIANS(#{latitude})) * COS(RADIANS(#{lat_attr})) * " +
"COS(RADIANS(#{lon_attr} - #{longitude}))" +
"))) + 360)"
bearing = "CAST(#{bearing} AS decimal) % 360"
distance = "#{Geocoder::Calculations.earth_radius} * 2 * ASIN(SQRT(" +
"POWER(SIN((#{latitude} - #{lat_attr}) * " +
"PI() / 180 / 2), 2) + COS(#{latitude} * PI() / 180) * " +
"COS(#{lat_attr} * PI() / 180) * " +
"POWER(SIN((#{longitude} - #{lon_attr}) * " +
"PI() / 180 / 2), 2) ))"
"POWER(SIN((#{latitude} - #{lat_attr}) * PI() / 180 / 2), 2) + " +
"COS(#{latitude} * PI() / 180) * COS(#{lat_attr} * PI() / 180) * " +
"POWER(SIN((#{longitude} - #{lon_attr}) * PI() / 180 / 2), 2) ))"
options[:order] ||= "#{distance} ASC"
default_near_scope_options(latitude, longitude, radius, options).merge(
:select => "#{options[:select] || '*'}, " +
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment