获取'Uncaught TypeError:无法读取未定义'错误的属性'geocode' [英] Getting 'Uncaught TypeError: Cannot read property 'geocode' of undefined ' error
问题描述
当我运行以下代码时,该代码取自 initialize
当我运行以下代码时,该代码取自 initialize
函数,而不是首先调用 codeLatLng(lat,lng)
的 initialize
函数.
When i am running the following code, which i have taken from this answer, i am getting Uncaught TypeError: Cannot read property 'geocode' of undefined
error in browser's console, why its happening because on the body load here initialize
function has to be called, and instead of calling initialize
function here codeLatLng(lat, lng)
is calling first.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Reverse Geocoding</title>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
//Get the latitude and the longitude;
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
codeLatLng(lat, lng)
}
function errorFunction(){
alert("Geocoder failed");
}
function initialize() {
geocoder = new google.maps.Geocoder();
}
function codeLatLng(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results)
if (results[1]) {
//formatted address
alert(results[0].formatted_address)
//find country name
for (var i=0; i<results[0].address_components.length; i++) {
for (var b=0;b<results[0].address_components[i].types.length;b++) {
//there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
//this is the object you are looking for
city= results[0].address_components[i];
break;
}
}
}
//city data
alert(city.short_name + " " + city.long_name)
} else {
alert("No results found");
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
</script>
</head>
<body onload="initialize()">
</body>
</html>
在初始化Geocoder-instance之前,无需等待onload事件.您是同步加载地图API的,因此该API(包括 google.maps.Geocoder
)在加载该API后立即可用.
There is no need to wait for the onload-event until you initialize the Geocoder-instance. You load the maps-API synchronously, so the API(including google.maps.Geocoder
) is available immediately after loading the API.
问题:当地理位置运行得太快,并且 navigator.geolocation.getCurrentPosition
的回调将在onload事件之前执行时,geocoder是 undefined
.
The issue: when geolocation runs too fast, and the callback of navigator.geolocation.getCurrentPosition
will be executed before the onload-event, geocoder is undefined
.
替换此行:
var geocoder;
有这行:
var geocoder = new google.maps.Geocoder();
这篇关于获取'Uncaught TypeError:无法读取未定义'错误的属性'geocode'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!