How to compare current user Latitude and Longitude with other user Latitude and Longitude

时间:2017-08-04 11:57:30

标签: angularjs google-maps firebase ionic-framework ionic2

I am a bit of loss on how to compare current user's lat and lng with other user lat and lng. I have tried the following. I want all user key who's lat and lng who is nearby the current user lat and lng. I have tried the following.

Here's my code:

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 userAddress = new LatLng(currentUserAddress);
      console.log("SURESH IS COOL");
      console.log(latlng);

    } 
  }); 

});           



  // 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 latitude = results[0].geometry.location.lat();
      var longitude = results[0].geometry.location.lng();
      var latlng1 = new LatLng(latitude, longitude);
      //var userAddress = new LatLng(currentUserAddress);
      //console.log("SURESH IS COOL");
      //console.log(latlng1);

       var distanceInMeters = google.maps.geometry.spherical.computeDistanceBetween (this.latlng, this.latlng1);

    if(distanceInMeters)
    {
        console.log("NEARBY ADDRESS that is near current user address");
        console.log(latlng1);
    }

    } 
  }); 



        });

    });




//END OF CURRENT USER 
}); 



  }

As you specifically see at this part i tried to compare the current user lat and lng with other user lat and lng. Using the if statement but nothing shows up in the console.

var distanceInMeters = google.maps.geometry.spherical.computeDistanceBetween (this.latlng, this.latlng1);

    if(distanceInMeters)
    {
        console.log("NEARBY ADDRESS that is near current user address");
        console.log(latlng1);
    }

1 个答案:

答案 0 :(得分:2)

Since you are using google maps, use the following:

    var distanceInMeters = google.maps.geometry.spherical.computeDistanceBetween (latlng, latlng1);
    if( distanceInMeters  < meterLimit ) //decide some threshold value for 'nearby' places.
    {
        console.log("NEARBY ADDRESS that is near current user address");
        console.log( distanceInMeters );
    }

For using the above, you need to append '&libraries=geometry' to your google map script tag's src value.

You can specify multiple libraries as a comma-separated list.

Edit:

If your priority is to show the nearest address, rather than nearby addresses, you should calculate all distances (in a loop), store them in an array, sort these into another array. Now compare the two to find out which element/address corresponds to lowest distance.