Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check two LatLng values using if condition in google map api v3?

I have developed a map with routes using DirectionsRenderer ... i need the destination marker to be bounced if origin and destination are at same place ... but when i check whether the two LatLng values are same using IF, the program does not execute the IF statement ... the destination marker is not getting bounced.. my coding is

        var myOptions = 
    {
        center: new google.maps.LatLng(default_latitude,default_longitude),
        zoom: 4,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("map"),myOptions);
    var latlng1=new google.maps.LatLng(glat,glon);
    var latlng2=new google.maps.LatLng(hlat,hlon);

    var marker = new google.maps.Marker({   
            icon: "http://www.googlemapsmarkers.com/v1/B/67BF4B/000000/00902C/", 
            position: new google.maps.LatLng(glat,glon),
            map: map
        });


    var marker1 = new google.maps.Marker({
                icon: " http://www.googlemapsmarkers.com/v1/A/67BF4B/000000/00902C/", 
                position: new google.maps.LatLng(hlat,hlon),
                map: map

            });  

    //THIS IF STATEMENT IS NOT WORKING ... WHAT CAN I USE INSTEAD OF THIS 
        if(latlng1==latlng2)
            marker1.setAnimation(google.maps.Animation.BOUNCE); 



    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("panel"));
    var request = {
      origin: latlng1,
      destination:latlng2,
      travelMode: google.maps.DirectionsTravelMode.DRIVING
      };

    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
       }
    }); 
like image 417
Ree Avatar asked Mar 13 '13 04:03

Ree


People also ask

How do I calculate distance using latitude and longitude on Google Maps?

The formula for calculating longitude distance is: "Dep = d. long * Cos Mid. Lat" Dep is the same thing as miles.

What is LatLng in Google map?

A LatLng is a point in geographical coordinates: latitude and longitude. Latitude ranges between -90 and 90 degrees, inclusive.


2 Answers

Try equals method in LatLng to compare 2 location

change if(latlng1==latlng2) to if(latlng1.equals(latlng2))

like image 153
Kris Avatar answered Oct 04 '22 21:10

Kris


To determine if 2 google.maps.LatLng objects are the same location, pick a distance (like 0.1 meters), calculate the distance between them, if it is less than the threshold, assume they are the same place. Comparing objects will only return true if they are the same identical object. Comparing 2 floating point numbers is problematic, and comparing two pairs of floating point objects is even more so.

var SameThreshold = 0.1;
if (google.maps.geometry.spherical.computeDistanceBetween(latlng1,latlng2) < SameThreshold)
   marker1.setAnimation(google.maps.Animation.BOUNCE); 

Be sure to include the geometry library

  • Developer's Guide
  • Reference
like image 25
geocodezip Avatar answered Oct 04 '22 22:10

geocodezip