Geolocation SQL query not finding exact location

前端 未结 2 1765
生来不讨喜
生来不讨喜 2021-01-06 07:38

I have been testing my geolocation query for some time now and I haven\'t found any issues with it until now.

I am trying to search for all cities within a given ra

2条回答
  •  半阙折子戏
    2021-01-06 07:41

    In your first query, I believe you've inverted the longitudes in the subtraction. The Spherical Law of Cosines is:

    d = acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(long2−long1))*R
    

    If lat1 is substituted with tblcity.latitude, long1 must be substituted with tblcity.longitude. I think you've accidentally substituted long2 in your query. Does this one work better?

    SELECT tblcity.city, tblcity.latitude, tblcity.longitude, 
    truncate((degrees(acos( sin(radians(tblcity.latitude)) 
    * sin(radians(45.266708)) 
    + cos(radians(tblcity.latitude)) 
    * cos(radians(45.266708)) 
    * cos(radians(-73.616257 - tblcity.longitude) ) ) ) 
    * 69.09*1.6),1) as distance 
    FROM tblcity HAVING distance < 10 ORDER BY distance desc 
    

    I haven't looked into your second query yet, but hopefully that helps.

提交回复
热议问题