我真不知道为什么会出现这种情况。
这是我的谷歌地图脚本标签:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places,geometry"></script>
如下所示,我已将纬度定义为lat()。它将错误指向第120行,即distanceInMeters
openMapPage()
{
// GETTING THE CURRENT USER ADDRESS FOR LATITUDE AND LONGTITUDE
var uid = firebase.auth().currentUser.uid;
var ref = firebase.database().ref("request/" + uid);
ref.once("value").then((snapshot) => { // <------ Here!
var a = snapshot.exists(); // true
var c = snapshot.hasChild("reqdetails"); // true
var d = snapshot.child('reqdetails').exists();
var requestsKey = snapshot.key;
var requestsValue = snapshot.val();
ref.once('value', (request) => {
var currentUserAddress = request.val().regdetails.address;
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': currentUserAddress}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
var latlng = new LatLng(latitude, longitude);
var meterLimit = latlng;
//var userAddress = new LatLng(currentUserAddress);
console.log("SURESH IS COOL");
console.log(meterLimit);
}
});
});
//END OF CURRENT USER
});
// GETTING THE ALL USER ADDRESS FOR LATITUDE AND LONGTITUDE
var ref1 = firebase.database().ref("request");
ref1.once("value").then((snapshot1) => { // <------ Here!
var a = snapshot1.exists(); // true
var c = snapshot1.hasChild("reqdetails"); // true
var d = snapshot1.child('reqdetails').exists();
var requestsKey = snapshot1.key;
var requestsValue = snapshot1.val();
snapshot1.forEach((childSnapshot) => { // <------ And here!
var requestKey = childSnapshot.key;
var requestValue = childSnapshot.val();
var reqdetails = requestValue.reqdetails;
var AllUserAddress = requestValue.regdetails.address;
//console.log("ALL USER ADDRESS");
//console.log(AllUserAddress);
var geocoder1 = new google.maps.Geocoder();
geocoder1.geocode( { 'address': AllUserAddress}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude1 = results[0].geometry.location.lat();
var longitude1 = results[0].geometry.location.lng();
var latlng1 = new LatLng(latitude1, longitude1);
//var userAddress = new LatLng(currentUserAddress);
//console.log("SURESH IS COOL");
//console.log(latlng1);
var distanceInMeters = google.maps.geometry.spherical.computeDistanceBetween (this.latlng, this.latlng1);
console.log("DISTANCE");
console.log(distanceInMeters);
}
});
});
});
}
错误看起来像这样,它一直在说lat。我的代码没有错误。顺便说一句,错误线120没有错误。
我仍然可以控制日志所有用户地址lat和lng
答案 0 :(得分:1)
我认为情况是您尚未在全局范围内定义this.latlng, this.latlng1
,因此无法访问它们,您尝试访问函数内声明的局部变量这就是为什么你无法获得distanceInMeters
的原因。所以试试这个:
public latlng; // DECLARE THEM ABOVE THE CONSTRUCTOR OF YOUR CLASS
public latlng1;
constructor(){
}
/*
DO THE FOLLOWING TO BOTH YOUR GEOCODERS:
- GET BOTH LATITUDE AND LONGITUDE OF YOUR.
- ACCESS YOUR GLOBAL latlng WITH this.
*/
geocoder1.geocode( { 'address': AllUserAddress}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
let latitude1 = results[0].geometry.location.lat();
let longitude1 = results[0].geometry.location.lng();
this.latlng1 = new LatLng(latitude1, longitude1);
let distanceInMeters = google.maps.geometry.spherical.computeDistanceBetween (this.latlng, this.latlng1);
console.log("DISTANCE");
console.log(distanceInMeters);
}
});
希望这有帮助