简体   繁体   中英

Error takes exactly 2 arguments (3 given)

Hi i am trying to plot points in a google map by indexing the dictionary with a foor loop, have this dictionary of 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}

And i have this code:

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

I get the error TypeError: add_point() takes exactly 2 arguments (3 given)

I am giving only latitude and longitude why it says i am giving 3 arguments?

Thanks in advance!

Edit: addint the class map:

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)

add_point seems to want the latitude & longitude as a single object; something like this:

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

addPoint() is supposed to take both coordinates as one parameter.

Another way to write map.addPoint(p) is Map.addPoint(map, p) . From the second example, you can see why you were getting an error by having too many parameters.

You can call your function like this to include both latitude and longitude in one parameter:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM