尝试映射经度/经度坐标,无法输入所需的参数



我有一个函数(来自pygmaps模块的mymap.addpoint),需要2个浮点参数。我有一个 for 循环,可以为列表中的每个城市生成纬度和经度。我想使用这些坐标将多个点(标记或图钉)添加到谷歌地图,但我不知道如何将它们输入为参数。map_regions是城市列表:

print map_regions
for item in map_regions:
        try:
            geo = Geocoder.geocode(item)
        except:
            pass
        else:
            point = (geo[0].coordinates)
        print point
regions_map.addpoint(lat, long)

我意识到上面的代码不包括 for 循环中的加点函数。我仍在尝试弄清楚如何在函数中只传递一次 2 个参数,然后再多次传递,如果这有任何意义的话。

这不起作用,因为需要两个参数:

regions_map.addpoint(point)

我试过这个,但似乎 2 个参数被视为字符串而不是浮动:

for item in map_regions:
        try:
            geo = Geocoder.geocode(item)
        except:
            pass
        else:
            point = (geo[0].coordinates)
            joint = ', '.join(map(str, point))
            split_point = joint.split(',', 2)
            lat = split_point[0]
            lon = split_point[1]
        print point
regions_map.addpoint(lat, long)

这是我得到的错误:

['MI', 'Allegan, MI', 'Alma, MI (All Digital)', 'Almont Township, MI', 'Alpena, MI',

>'Arnold Lake/Hayes, MI (All Digital)', 'Au Gres, MI']

(44.3148443, -85.60236429999999)

(42.5291989, -85.8553031)

(43.3789199, -84.6597274)

(42.9450131, -83.05761559999999)

(45.0616794, -83.4327528)

(45.0616794, -83.4327528)

(44.0486294, -83.6958161)

回溯(最近一次调用):

文件 "/Users/digital1/Dropbox/Programming/Map_Locations/gmaps.py",第 82 行,在 gmaps_mapit()

文件"/Users/digital1/Dropbox/Programming/Map_Locations/gmaps.py",第 78 行,第>gmaps_mapit 行 regions_map.draw('./mymap.html')

文件 "build/bdist.macosx-10.6-intel/egg/pygmaps.py",第 48 行,绘制中

文件 "build/bdist.macosx-10.6-intel/egg/pygmaps.py",第 83 行,在绘制点中

文件"build/bdist.macosx-10.6-intel/egg/pygmaps.py",第 129 行,在绘图点中

类型错误:需要浮点参数,而不是 str

如何使用 for 循环(或其他方式)将坐标作为函数的参数传递以生成多个点?

我是一个相当不错的谷歌人,但这个很难。我什至不确定如何搜索它。谢谢

错误说addpoint()函数需要参数作为浮点数,您正在将它们作为字符串传递。您所需要的只是将它们解析为浮点数:

regions_map.addpoint(float(lat), float(long))

最新更新