Google Maps Api 3: Uncaught TypeError: Cannot call

2019-05-29 00:46发布

问题:

I have been trying to get my geocoder working this morning for the following URL but can't seem to figure out what I am doing wrong: http://www.dlplaw.com/media/map.html

When clicking encode, the geocoder is doing it's job and returning the correct Lat / Long coordinates but it gets hung up on setCenter.

Uncaught TypeError: Cannot call method 'setCenter' of undefined

I tried to follow the Google example here and can't understand why this won't work: http://code.google.com/apis/maps/documentation/javascript/examples/geocoding-simple.html

Thanks in advance.

回答1:

map is not in scope. You declare map twice – once in the global scope and once in initialize, but you only set the map in initialize (the global map is shadowed).

Remove the var from var map = new google.maps.Map(...); (but not the var map; at the top).