这个问题已经在这里有了答案: > from list of tuples, get tuple closest to a given value 3个
我正在使用Python并执行以下操作,给定一个元组坐标列表,找到最接近指定坐标的坐标(Google Maps Coords)).
但与我的代码中的Google地图相比,最近的坐标不准确.请帮我.
这是我的代码
def find_coords(c, l):
tmp_list = []
(x,y) = l[0]
for (a,b) in l[1:]:
if (x-c[0])**2 + (y-c[1])**2 > (a-c[0])**2 + (b-c[1])**2:
(x,y) = (a,b)
tmp_list.append((x,y))
return tmp_list
ccoordinate_list = [(11.6702634, 72.313323), (11.6723698, 78.114523), (31.67342698, 78.465323), (12.6702634, 72.313323), (12.67342698, 75.465323)]
coordinate = (11.6723698, 78.114523)
while coordinate_list[1:]:
coordinate_list = find_coords(coordinate, coordinate_list)
解决方法:
如果要查找最近的地理坐标,则应使用特定的地理坐标结构(请参见geopy).在这种情况下,我提出以下解决方案:
i