在google maps中将zipcode转换为latlng时出现Javascript错误
我正在使用随机用户生成器,每次单击按钮时都会得到一个邮政编码。这很好。但当我尝试将zipcode发送到google maps geolocation时,我得到:在google maps中将zipcode转换为latlng时出现Javascript错误,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我正在使用随机用户生成器,每次单击按钮时都会得到一个邮政编码。这很好。但当我尝试将zipcode发送到google maps geolocation时,我得到: Uncaught ReferenceError: zipcode is not defined script.js:22 有什么帮助吗 Index.html <!DOCTYPE html> <html> <head> <meta name="viewport" content
Uncaught ReferenceError: zipcode is not defined script.js:22
有什么帮助吗
Index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?v&sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(16.4706001, -33.6728973),
zoom:3,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
<div id="button">
<button id="loadbutton">Click to Load</button>
</div>
<script type="text/javascript" src="js/script.js"></script>
</body>
</html>
您在第一个函数中定义了
zipcode
,并尝试在另一个函数中访问它,其中变量未知。
您需要将zipcode
变量放入全局范围,以便其他函数也可以访问它
Ashley Medway指出:latlng()
应该在success
函数中触发。这是因为AJAX是异步的,在触发函数时可能无法完成
var randomuserURL = 'http://api.randomuser.me/';
var zipcode;
var myButton = document.getElementById('loadbutton');
myButton.onclick = loadAJAX;
function loadAJAX() {
$.ajax({
url: randomuserURL,
dataType: 'json',
success: function (data) {
zipcode = data.results[0].user.location.zip;
latlng();
}
});
}
function latlng() {
var lat = '';
var lng = '';
var address = zipcode;
geocoder.geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
lat = results[0].geometry.location.lat();
lng = results[0].geometry.location.lng();
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
alert('Latitude: ' + lat + ' Logitude: ' + lng);
}
另外:您可能需要定义var geocoder=new google.maps.geocoder()代码>和包含
试试这个
function loadAJAX() {
var zipcode;
$.ajax({
url: randomuserURL,
dataType: 'json',
success: function (data) {
zipcode = data.results[0].user.location.zip;
latlng(zipcode);
}
});
}
而且
function latlng(zipcode) {
............
...........
}
您需要在全局范围中定义zipcode
,而不是在函数中。另外latlng()
应该出现在success块中,因为ajax代码是异步的。我已经修复了范围变量并在success中包含latlng(),但现在我得到了“geocoder未定义”。Geocoder是谷歌地图的呼叫。有什么线索可以解决这个问题吗?我已经解决了。问题是定义var geocode=new google.maps.Geocoder();最后是工作,但在第一次点击时,我得到零结果,从第二次点击时,我得到纬度和经度。此外,当我使用zipcode搜索谷歌地图时,我需要在第一次输入zipcode时搜索两次。希望这对你有帮助。谢谢你,这也起作用了。但是我代码上的错误太多了。现在我已经修复了scope变量,然后定义var geocode=new google.maps.Geocoder()
终于开始工作了,但现在第一次点击时,我得到了零结果
,第二次点击时,我得到了纬度和经度。所以,当我去谷歌地图用zipcode搜索时,我需要在第一次输入zipcode时搜索两次。希望这有帮助。
function latlng(zipcode) {
............
...........
}