MySQL - Find points within radius from database

前端 未结 3 749
无人及你
无人及你 2020-12-10 07:45

I have a table which has a POINT column containing the latitude and longitude of various locations.

I then also have a users location from geo-location

3条回答
  •  Happy的楠姐
    2020-12-10 07:57

    May be this help for you, https://ru.scribd.com/presentation/2569355/Geo-Distance-Search-with-MySQL

    For Django I use this

        dist = 20 #дистанция 20 км
        mylon = 51.5289156201 # долгота центра
        mylat = 46.0209384922 # широта 
        lon1 = mylon-dist/abs(math.cos(math.radians(mylat))*111.0) # 1 градус широты = 111 км
        lon2 = mylon+dist/abs(math.cos(math.radians(mylat))*111.0)
        lat1 = mylat-(dist/111.0)
        lat2 = mylat+(dist/111.0)
        profiles = UserProfile.objects.filter(lat__range=(lat1, lat2)).filter(lon__range=(lon1, lon2))
    

    It search all users in squar 20km.

提交回复
热议问题