错误需要2个参数(给定3个)

时间:2017-12-08 00:09:35

标签: python dictionary for-loop

您好我正试图通过使用foor循环索引字典来绘制谷歌地图中的点数,请使用此lat字典:long

 latlon = {32.1243973: -101.8856125, 32.666694: -104.233502, 32.222012:
 -101.819122, 32.53151: -103.353018, 32.668496: -104.235722, 32.1041336: -101.8818588}

我有这段代码:

if __name__ == "__main__":
        map = Map()
        for latitude, longitude in latlon.iteritems():
            map.add_point(latitude, longitude)

我收到错误 TypeError:add_point()只需要2个参数(给定3个)

我只给出经度和经度为什么它说我给出3个参数?

提前致谢!

编辑:添加类地图:

class Map(object):
    def __init__(self):
        self._points = []
    def add_point(self, coordinates):
        self._points.append(coordinates)
    def __str__(self):
        centerLat = sum(( x[0] for x in self._points )) / len(self._points)
        centerLon = sum(( x[1] for x in self._points )) / len(self._points)
        markersCode = "\n".join(
            [ """new google.maps.Marker({{
                position: new google.maps.LatLng({lat}, {lon}),
                map: map
                }});""".format(lat=x[0], lon=x[1]) for x in self._points
            ])
        return """
            <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
            <div id="map-canvas" style="height: 100%; width: 100%"></div>
            <script type="text/javascript">
                var map;
                function show_map() {{
                    map = new google.maps.Map(document.getElementById("map-canvas"), {{
                        zoom: 8,
                        center: new google.maps.LatLng({centerLat}, {centerLon})
                    }});
                    {markersCode}
                }}
                google.maps.event.addDomListener(window, 'load', show_map);
            </script>
        """.format(centerLat=centerLat, centerLon=centerLon,
                   markersCode=markersCode)

2 个答案:

答案 0 :(得分:1)

add_point似乎想要纬度&amp;经度作为单一对象;像这样的东西:

for ll in latlon.iteritems(): 
    map.add_point(ll)

答案 1 :(得分:0)

addPoint()应该将两个坐标作为一个参数。

撰写map.addPoint(p)的另一种方式是Map.addPoint(map, p)。从第二个示例中,您可以看到有太多参数导致错误的原因。

您可以像这样调用您的函数,在一个参数中包含纬度和经度:

for i in latlon.iteritems:
    map.addPoint(i)