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);
}
});
The formula for calculating longitude distance is: "Dep = d. long * Cos Mid. Lat" Dep is the same thing as miles.
A LatLng is a point in geographical coordinates: latitude and longitude. Latitude ranges between -90 and 90 degrees, inclusive.
Try equals method in LatLng to compare 2 location
change if(latlng1==latlng2)
to if(latlng1.equals(latlng2))
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With