mysql - Which unit is `distance` returning in this SQL query -
i'm messing latitude/longitude related code in google maps api provides me following sql statement
select id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) distance marker having distance < 25 order distance limit 0 , 20;
link google maps example here: https://developers.google.com/maps/articles/phpsqlsearch_v3
however distance returned on 7,000, while know of entries in database within 50 miles of each other. i've verified using http://www.movable-type.co.uk/scripts/latlong.html goes on haversine formula.
so i'm curious i'm doing wrong provided query, returns:
(image) http://gyazo.com/ece247747616c5a412edd40c82c4b0ce -- (failed upload image, format not accepted???).
all points compared from
long: 39.410870 lat: -107.102180
here's full query:
select id,`user_long`,`user_lat`, ( 3959 * acos( cos( radians(39.410870) ) * cos( radians( `user_lat` ) ) * cos( radians( `user_long` ) - radians(-107.102180) ) + sin( radians(39.410870) ) * sin( radians( `user_lat` ) ) ) ) distance `accounts` order distance limit 0 , 20
as can see in results, it's strange, because when compared itself, distance > 7000.
id: 1 long: 39.410870 lat: -107.102180 dist: 7923.067131806453
units in miles.
it looks you've got latitude , longitude reversed/swapped.
valid range latitude -90 +90, can't -107 (degrees).
if mean specify location in colorado west of denver, halfway grand junction, swap values latitude , longitude.
the "great circle calculation" looks correct, it's going return miles, since multiplying 3959. (you'd need replace constant different 1 distance units other miles.)
it looks you've got fixed latitude , longitude correctly place in expression... suspect it's values in user_lat
, user_lng
swapped/reversed.
Comments
Post a Comment