0
我的目标是检索用户的纬度/经度位置,然后在我的位置列表中找到最近的纬度/经度。下面是我所拥有的,我相信它是有效的,但我不确定这是否是达到我想要的最终结果的最佳方法。查找最近的值对与Python3
我通过位置列表将其转换为绝对值,然后减去用户的位置值以获得距离。如果X oy Y小于先前记录的X或Y,则距离变量将被新值更新。
这似乎是行得通的,但就像我说的,我不确定我是否以最好的方式解决问题。我的位置列表会每隔一段时间更新一次,但不会超过100个可能的位置。
非常感谢您的时间。
locations = [(-71.43994800000002,41.6919549),
(-71.61075089999997,41.577545),
(-71.06653670000003,42.41383099999999),
(-71.41283429999999,41.8239891),
(-71.05888010000001,42.3600825),
(-74.00594130000002,40.7127837)]
userlocation = (-71.28254930000003,41.7303793)
distance = [999,999] #initial value holder for distance
for location in locations:
x = abs(location[0]) # absolute value of latitude
y = abs(location[1]) #absolute value of longitude
xu = abs(userlocation[0]) #absolute value of user's latitude
yu = abs(userlocation[1]) #absolute value of user's longitude
dx = x-xu #Subtract user from location X
dy = y-yu #subtract user from location Y
if dx < distance[0]: #if distance X is less than the current distance value
distance[0] = dx #update with new values
distance[1] = dy
continue #go to the next one
if dy < distance[1]: #if distance Y is less than the current distance value
distance[0] = dx #update with new values
distance[1] = dy
continue #go to the next one
print(distance) #print the end smallest result
经度和纬度都没有X/Y。您不能简单地计算两个此类坐标之间的欧几里德距离或曼哈顿距离等。 Furterhermore还有一个wraparond。 –
你需要多精确?因为欧几里得距离可能不够精确 - 例如,两极的纬度需要〜111.7公里,而在赤道它只覆盖〜110.6公里。 – zwer
我知道,当我扩大到整个地球时,我们无法将这些坐标表示为网格,但是我正在美国的一个小地区工作,所以我认为在我的限制区内它可能适用于相对距离。我的假设是否不正确? – Reizvoller