【问题标题】:Randomly pick a new location between a range of distance from a given location在距给定位置的距离范围内随机选择一个新位置
【发布时间】:2020-12-10 03:28:26
【问题描述】:

我有什么:

  • 纬度/经度
  • 以千米为单位的最小距离,例如 0.3
  • 以公里为单位的最大距离,例如 1.5

我必须随机选择一个距离给定位置至少 0.3 公里且最大 1.5 公里的新位置。即,我必须选择的这个新位置可以在任何方向距给定位置 0.3 公里到 1.5 公里之间。

注意:我必须用 Elixir(编程)语言来实现,但数学公式或伪代码都可以

【问题讨论】:

    标签: location coordinates latitude-longitude direction geopositioning


    【解决方案1】:

    另一个answer 帮助我在任何方向上随机生成一个统一的位置。以下是步骤

    • dx = [0.3..0.7] x cos([0..2] x pi)
    • dy = [0.3..0.7] x sin([0..2] x pi)
    • 来自this answer的一些帮助
    • r_earth = 6378

    然后你就可以了

    new_latitude  = latitude  + (dy / r_earth) * (180 / pi);
    new_longitude = longitude + (dx / r_earth) * (180 / pi) / cos(latitude * pi/180);
    

    这是 Elixir 语言的实现

    def random_location_in_given_range(latitude, longitude, min_distance_in_km, max_distance_in_km) do
        random_distance =
        Enum.random(round(min_distance_in_km * 1000)..round(max_distance_in_km * 1000)) / 1000
    
        random_radian = Enum.random(0..200) / 100
        # dx = [0.3..0.7] x cos([0..2] x pi)
        dx = random_distance * :math.cos(random_radian * :math.pi())
        # dy = [0.3..0.7] x sin([0..2] x pi)
        dy = random_distance * :math.sin(random_radian * :math.pi())
        # radius of the earth in kilometer
        r_earth = 6378.137
    
        new_latitude = latitude + dy / r_earth * (180 / :math.pi())
    
        new_longitude =
        longitude + dx / r_earth * (180 / :math.pi()) / :math.cos(latitude * :math.pi() / 180)
    
        {new_latitude, new_longitude}
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 2014-08-15
      • 1970-01-01
      相关资源
      最近更新 更多