python3产生随机经纬度

import random
import math

# 这里的参数包括一个基准点,和一个距离基准点的距离
def generate_random_gps(base_log=None, base_lat=None, radius=None):
    radius_in_degrees = radius / 111300
    u = float(random.uniform(0.0, 1.0))
    v = float(random.uniform(0.0, 1.0))
    w = radius_in_degrees * math.sqrt(u)
    t = 2 * math.pi * v
    x = w * math.cos(t)
    y = w * math.sin(t)
    longitude = y + base_log
    latitude = x + base_lat
    # 这里是想保留6位小数点
    loga = '%.6f' % longitude
    lata = '%.6f' % latitude
    return loga, lata
    
log1, lat1 = generate_random_gps(base_log=120.7, base_lat=30, radius=1000000)
print(log1)

猜你喜欢

转载自blog.csdn.net/weixin_43169720/article/details/86016179