Python:-给定元组坐标列表,找到最接近指定坐标的坐标(谷歌地图坐标))

标签 python google-maps distance

<分区>

我正在使用 Python 并完成了以下操作,给定元组坐标列表,找到与指定坐标(Google map 坐标)最近的坐标。

但与我的代码中的谷歌地图相比,最近的坐标不准确。请帮助我。

这是我的代码

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 )。针对这种情况,我提出以下解决方案:

import geopy
import geopy.distance
# your data    
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)
# the solution
pts = [ geopy.Point(p[0],p[1]) for p in ccoordinate_list ]
onept = geopy.Point(coordinate[0],coordinate[1])
alldist = [ (p,geopy.distance.distance(p, onept).km) for p in pts ]
nearest_point = min(alldist, key=lambda x: (x[1]))[0] # or you can sort in by distance with sorted function

请注意,地球上的欧几里得度量(如您的示例)可能不正确。

关于Python:-给定元组坐标列表,找到最接近指定坐标的坐标(谷歌地图坐标)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28367177/

相关文章:

python - 添加带条件着色的水平线

python - 不允许我在 blender 中读取 .csv 文件

python - Pandas df 使用准备好的样式写入 Excel 模板

ios - 在谷歌地图上定位标记

javascript - 攻击者能否用完其他站点的 Google map 配额?

php - 如何使用Elasticsearch将过滤器与距离结合起来以进行搜索?

python - 如何查找图表上第 n 个最接近坐标的列表

python - Django 调试服务器的代码覆盖率

javascript - Google map API 多个标记

c - 如何从 ZLIB 压缩器获取 <距离,长度> 对