为什么我无法获得此代码的坐标?无法从此代码中获取纬度和经度
错误是:ReferenceError: Can't find variable: card_Latitude
。
我把代码从这里:How to get longitude and latitude of a city/country inputted through an input box?
我不明白为什么。
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
var card_FullAddress = "Canada";
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': card_FullAddress
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var card_Latitude = results[0].geometry.location.lat();
var card_Longitude = results[0].geometry.location.lng();
} else {
var card_Latitude = "ERR";
var card_Longitude = "ERR";
}
});
console.log(card_Latitude);
console.log(card_Longitude);
</script>
因为'card_Latitude'是** **本地的回调函数。如果你仔细观察,你会注意到你把'console.log' *放在回调的外部。这是一个更简单的例子:'function foo(){var bar = 42; };的console.log(巴);'。看起来你对异步代码不熟悉,所以我推荐阅读:http://stackoverflow.com/q/23667086/218196 – 2014-10-02 15:31:05
'card_Latitude'存在于不同的范围内。 – gdoron 2014-10-02 15:31:36
那么如何在外面访问它呢?我试图删除'var',但同样的错误。 – Bonito 2014-10-02 15:32:57