可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Here is the Listener I'm adding -
var map; var geocoder; function initialize() { var myOptions = { zoom: 8, center: new google.maps.LatLng(22, 88), mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); } //google.maps.event.addDomListener(window, 'load', initialize); google.maps.event.addListener(map, 'click', function(event) { geocoder = new google.maps.Geocoder(); geocoder.geocode( { 'latLng': event.latLng}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { alert(results); } else { alert("Geocode was not successful for the following reason: " + status); } }); });
But it is generating this error in console -
Uncaught TypeError: Cannot read property '__e3_' of undefined Ke Ie R.addListener (anonymous function)
Tried searching. But got no solution.
回答1:
Move your listener into the initializer function.
回答2:
It's hard to tell with only a small part of your code, but that sounds like it cannot find "map".
Either your map variable is not global, or somehow accessible by that code OR the map was not created with:
map = new google.maps.Map(....)
before you try and add a new click listener.
回答3:
I don't know if this may be an issue for you since we don't see the part of your code where you are linking to the google maps library, but I just discovered the reason I was getting this error was from trying to include the "visualizations" library. Luckily I didn't need to use it anymore.
I had this:
and changed it to this:
I no longer get the e3 error.
By the way, this error for me was not consistent and I could not figure out how to replicate it. I had to sit there and hit refresh over and over until the error occurred.
I hope this helps someone.
回答4:
This happens when loading your data asynchronous. You can avoid that by including your document.ready code into the ajaxStop function
$().ready(function() { $(document).ajaxStop(function () { /* your code here ?*/ }); });
回答5:
In my case it was that the global map variable had been overwritten in the initializer but I wanted to use that global variable in another function and this caused the problem.
回答6:
Yes, second that... The map variable must be declared outside and before its use. As the google maps listener cannot find it.
回答7:
Something like this?
var lati,longt; //contains latitude and longitude func newLatLOng(){ get new value of latLong initMap(); } var map = 0; function initMap() { if (map ===0) { //load first time lati= 28.5072216; longt= 77.4971153; map = new google.maps.Map(document.getElementById('map'), { center: {lat: lati, lng: longt}, zoom: 13 }); }else { var geolocation = { lat:lati, lng: longt }; google.maps.event.trigger(map, 'resize'); map.setCenter(geolocation); }
回答8:
In my case google.maps.event.addListener(marker, 'click', function() {
The marker variable was undefined. And hence the error. Might be useful for someone.