python算一个位置3千米内的最大,小经纬度

charger_area_list = ChargingArea.get_charger_by_lat_and_lng(lat, lng, round)

这是函数调用入口,也要穿相应参数 经纬度 ,范围 例:3000

    @classmethod
    def get_charger_by_lat_and_lng(cls, lat, lng, round=20000):
        min_latitude, min_longitude, max_latitude, max_longitude = get_around(lat, lng, round)
        charger_list = cls.objects.filter(latitude__range=[min_latitude, max_latitude], longitude__range=[min_longitude,max_longitude]).all()

        return charger_list

这是判断函数,这里要把刚刚传过来的参数,传到这里get_around(lat, lng, round)



import math
import logging
logger = logging.getLogger('charger')

def get_around(latitude, longitude, radius_mile=2000):
    if latitude and longitude:
        float_latitude = float(latitude)
        float_longitude = float(longitude)
        degree = (24901 * 1609) / 360.0

        dpm_latitude = 1 / degree
        radius_latitude = dpm_latitude * radius_mile
        min_latitude = float_latitude - radius_latitude
        max_latitude = float_latitude + radius_latitude

        mpd_longitude = degree * math.cos(float_latitude * (math.pi / 180))
        dpm_longitude = 1 / mpd_longitude
        radius_longitude = dpm_longitude * radius_mile
        min_longitude = float_longitude - radius_longitude
        max_longitude = float_longitude + radius_longitude

        return min_latitude, min_longitude, max_latitude, max_longitude
    return 0, 0, 0, 0


if __name__ == '__main__':
    logger.debug(get_around(121.32, 32.121))  #自己测试

打印print(get_around(121.32, 32.121))就会拿到最大经纬度,最小经纬度

地球的周长是24901英里,360是度数,1609地球半径吧可能

math.pi = 圆周率
cos() = 返回x的弧度的余弦值。

posted on 2021-03-09 17:16  G_bk  阅读(167)  评论(0)    收藏  举报