无法读取lat未定义的属性。

时间:2022-04-17 23:00:18

I have seriously no idea why is this showing up.

我真的不知道为什么会出现这种情况。

This is my google map script tag:

这是我的谷歌地图脚本标签:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places,geometry"></script>

As you can see below i have defined the latitude as lat(). It's pointing the error at line 120 which is at distanceInMeters

正如您在下面看到的,我已经定义了纬度为lat()。它指向了120行的误差,这是距离单位。

  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);


    } 
  }); 



        });

    });







  }

The error looks something like this, It's keep saying lat. And my code has no error. Btw the error line 120 has no error though. 无法读取lat未定义的属性。

误差看起来是这样的,它一直在说lat。我的代码没有错误。但是,错误行120没有错误。

I can still console log all my user address lat and lng

我仍然可以控制我的用户地址lat和液化天然气。

无法读取lat未定义的属性。

1 个解决方案

#1


1  

I think the case is that you haven't defined this.latlng, this.latlng1 in your global scope, so you can't access them, you're trying to access local variables declared inside your functions that's why you can't get distanceInMeters. So try this:

我认为你没有定义这个。latlng,这。在全局范围内的latlng1,因此无法访问它们,您试图访问在函数中声明的局部变量,这就是为什么您不能获得距离。那么试试这个:

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);
  } 
});

Hope this helps

希望这有助于

#1


1  

I think the case is that you haven't defined this.latlng, this.latlng1 in your global scope, so you can't access them, you're trying to access local variables declared inside your functions that's why you can't get distanceInMeters. So try this:

我认为你没有定义这个。latlng,这。在全局范围内的latlng1,因此无法访问它们,您试图访问在函数中声明的局部变量,这就是为什么您不能获得距离。那么试试这个:

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);
  } 
});

Hope this helps

希望这有助于