我是谷歌地图的新用户,我有一个错误Uncaught TypeError: Cannot read property '__e3_' of undefined
有人可以告诉我我的代码有什么问题。 。
错误指向
google.maps.event.addListener(map, 'dragend', function () {
if (strictBounds.contains(map.getCenter())) return;
和
window.onload = function () {
initialize();
codeAddress();
}
这是我的Initialize()
中的代码var geocoder, infoBubble;
var map;
//var mgr;
function initialize() {
var minZoomLevel = 4;
var zooms = 7;
geocoder = new google.maps.Geocoder();
$.getJSON('/Dashboard/LoadAddress', function Geocode(address) {
$.each(address, function () {
var currValAddress = this["AddressLine1"];
geocoder.geocode({ 'address': currValAddress }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map = new google.maps.Map(document.getElementById('map'), {
zoom: minZoomLevel,
center: currValAddress,
mapTypeId: google.maps.MapTypeId.ROADMAP
})
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
})
});
});
// Bounds for North America
var strictBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(15.70, -160.50),
new google.maps.LatLng(68.85, -55.90)
);
// Listen for the dragend event
google.maps.event.addListener(map, 'dragend', function () {
if (strictBounds.contains(map.getCenter())) return;
// We're out of bounds - Move the map back within the bounds
var c = map.getCenter(),
x = c.lng(),
y = c.lat(),
maxX = strictBounds.getNorthEast().lng(),
maxY = strictBounds.getNorthEast().lat(),
minX = strictBounds.getSouthWest().lng(),
minY = strictBounds.getSouthWest().lat();
if (x < minX) x = minX;
if (x > maxX) x = maxX;
if (y < minY) y = minY;
if (y > maxY) y = maxY;
map.setCenter(new google.maps.LatLng(y, x));
});
// Limit the zoom level
google.maps.event.addListener(map, 'zoom_changed', function () {
if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel);
});
}
答案 0 :(得分:1)
地理编码与getJSON调用一样是异步的。在定义和初始化之前,您将开始使用map变量。
发生了什么:
从服务器返回其他地理编码器结果,每次重新创建并重新初始化地图
var geocoder, infoBubble;
var map;
//var mgr;
function initialize() {
var minZoomLevel = 4;
var zooms = 7;
geocoder = new google.maps.Geocoder();
$.getJSON('/Dashboard/LoadAddress', function Geocode(address) {
// ??? do you really want to create a new map for each address?
$.each(address, function () {
var currValAddress = this["AddressLine1"];
geocoder.geocode({ 'address': currValAddress }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map = new google.maps.Map(document.getElementById('map'), {
zoom: minZoomLevel,
center: currValAddress,
mapTypeId: google.maps.MapTypeId.ROADMAP
})
// Bounds for North America
var strictBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(15.70, -160.50),
new google.maps.LatLng(68.85, -55.90)
);
// Listen for the dragend event
google.maps.event.addListener(map, 'dragend', function () {
if (strictBounds.contains(map.getCenter())) return;
// We're out of bounds - Move the map back within the bounds
var c = map.getCenter(),
x = c.lng(),
y = c.lat(),
maxX = strictBounds.getNorthEast().lng(),
maxY = strictBounds.getNorthEast().lat(),
minX = strictBounds.getSouthWest().lng(),
minY = strictBounds.getSouthWest().lat();
if (x < minX) x = minX;
if (x > maxX) x = maxX;
if (y < minY) y = minY;
if (y > maxY) y = maxY;
map.setCenter(new google.maps.LatLng(y, x));
});
// Limit the zoom level
google.maps.event.addListener(map, 'zoom_changed', function () {
if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel);
});
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
})
});
});
}