未定义地理编码中的变量,如何正确调用它们
Variables from geocoding not defined, how to call them correctly?
我正在尝试学习javascript并使用Google Maps API,似乎我陷入了一个(希望)简单的问题。
我编写了此函数来获取地理编码坐标:
function codeAddress() {
var st = document.getElementById('start').value;
var dest = document.getElementById('end').value;
geocoder.geocode( { 'address': st}, function(results, status) {
var startCoded = results[0].geometry.location;
var startLng = results[0].geometry.location.lng();
var startLat = results[0].geometry.location.lat();
document.getElementById("content1").value = startCoded;
});
geocoder.geocode( { 'address': dest}, function(results, status) {
var endCoded = results[0].geometry.location;
var endLng = results[0].geometry.location.lng();
var endLat = results[0].geometry.location.lat();
document.getElementById("content2").value = endCoded;
if (status == google.maps.GeocoderStatus.OK) {
var lngSpan = startLng - endLat;
var latSpan = startLat - endLng;
console.log(endCoded);
}
});
}
但是我收到此错误: 引用错误: 未定义启动Lng
我知道这与我需要回调来阅读它们的事实有关,但我不知道该怎么做。
提前谢谢。
我认为这就是您声明变量的方式(它们是函数的本地而不是全局的)。尝试:
var startLng, startLat, endLng, endLat;
function codeAddress() {
var st = document.getElementById('start').value;
var dest = document.getElementById('end').value;
geocoder.geocode( { 'address': st}, function(results, status) {
var startCoded = results[0].geometry.location;
startLng = results[0].geometry.location.lng();
startLat = results[0].geometry.location.lat();
document.getElementById("content1").value = startCoded;
});
geocoder.geocode( { 'address': dest}, function(results, status) {
var endCoded = results[0].geometry.location;
endLng = results[0].geometry.location.lng();
endLat = results[0].geometry.location.lat();
document.getElementById("content2").value = endCoded;
if (status == google.maps.GeocoderStatus.OK) {
var lngSpan = startLng - endLat;
var latSpan = startLat - endLng;
console.log(endCoded);
}
});
}
相关文章:
- 为什么谷歌地图API's方法未从RequireJS中正确调用
- 如何正确调用Javascript中的匿名函数
- 如何从jQuery对象文本正确调用服务器端ASP.NET并将数据返回给jQuery
- 按钮未正确调用函数
- MVC 下拉列表更改事件未正确调用控制器中的操作
- AJax没有正确调用php文件,html表单
- JavaScript函数无法正确调用
- 按键/更新未正确调用
- 刷新函数未正确调用 d3
- 使用两个变量正确调用函数
- 未正确调用 jQuery 图像加载函数
- NG-控制器即使通过正确调用也无法工作
- 如何正确调用 Page.ClientScript 中的函数
- 在谷歌地图Javascript API v2(用于营业执照)中,如何正确调用地图API
- 为什么在点击时正确调用 $(this),但在悬停时没有
- 我是否正确调用了 JavaScript 文件
- 为什么我的jQuery函数没有被正确调用
- 如何正确调用此函数
- 即使在正确调用函数的情况下,对象也需要在javascript中出现错误
- 如何正确调用此承诺链