我的目标是检索用户的纬度/经度位置,然后在我的位置列表中找到最近的纬度/经度。以下是我所拥有的,我相信它有效,但我不确定这是否是达到我想要的最终结果的最佳方法。
我浏览将它们转换为绝对值的位置列表,然后减去用户的位置值以获得距离。如果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
答案 0 :(得分:0)
我会尝试获得真正的距离,然后以一种黑客的方式比较距离Calculate distance between two latitude-longitude points? (Haversine formula)然后
def calculate_distance(lat1, lon1, lat2, lon2):
# go to the link to use the implementation
pass
locations = []
user_loc = (-71.28254930000003,41.7303793)
ulat, ulon = user_loc
res = map(lambda x: (calculate_distance(ulat, ulon, x[0], x[1]), x), locations)
print min(res, key=lambda x: x[0])