javascript - 在 JavaScript 中从 Geocoding Google-Api 向表中添加地址
问题描述
我正在尝试制作家长控制系统,该系统允许家长在 WebService 中跟踪他的孩子位置历史记录。
我有将位置发送到 Google Firebase 中的实时数据库的移动应用程序。这个位置(纬度和经度)从 Firebase 发送到我的 Javascript 代码,我想将它们放在我的 HTML 中的表格中。我不想把经度和纬度放在表中,但我想根据这个位置保存地址。这就是我使用 Google Geolocations API 的原因。问题是,当我在要放入表中的变量中设置此地址时,此变量似乎未定义。我认为这个变量的范围可能有问题,但我无法管理。在此先感谢您的帮助。我把我的JS代码放在下面:
var longitude;
var latitude;
var address;
//GETING DATA FROM FIREBASE
$(document).ready(function(){
var rootRef = firebase.database().ref().child("locations");
rootRef.on("child_added", snap => {
time = snap.child("time").val();
longitude = snap.child("longitude").val();
latitude = snap.child("latitude").val();
latitude = parseFloat(latitude);
longitude = parseFloat(longitude);
//CONVERTING LATITUDE AND LONGITUDE INTO ADDRESS
var geocoder = new google.maps.Geocoder();
var location = new google.maps.LatLng(latitude, longitude);
geocoder.geocode({'latLng': location}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
address = results[0].formatted_address;
//HERE ADDRESS IS DISPLAYING GOOD
}
});
//PUT ADDRESS INTO TABLE (THERE IS PROBLEM - ADDRESS IS Undefined)
$("#table_body").append("<tr><td>" + time + "</td><td>" + address + "</td><td>");
})
})
解决方案
尝试jQuery.append()
在条件表达式内部移动:
coder.geocode({'latLng': location}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var tempAddress = results[0].formatted_address;
$("#table_body").append("<tr><td>" + time + "</td><td>" + tempAddress + "</td><td>");
}
});
推荐阅读
- java - 如何从 Anylogic 的代理群体中选择特定代理?
- node.js - 使用 Mongoose 方法增加字段
- typescript - 为打字稿配置 TypeORM 会忽略反射元数据
- laravel - Laravel 8 试图获取非对象错误的属性“类型”
- excel - 如何在Excel VBA中使用winsock控件
- python - 'post' 对象在 django 中不可迭代,它显示错误,但我没有得到代码上的问题
- python - Selenium Python 将文件上传到 Google Keep
- javascript - 您如何一次一项地创建这个树形数据结构?
- javascript - 类型'{类型:任何;必需:[真,字符串];独特的:真实的;匹配:(字符串|正则表达式)[];}' 不可分配给类型 'string'
- javascript - 根据用户输入引用类中的不同对象