如何使用google map api v3中的if条件检查两个LatLng值?

Ree*_*Ree 5 javascript google-maps if-statement google-maps-api-3

我使用DirectionsRenderer开发了一个带路线的地图...如果原点和目的地在同一个地方,我需要退回目标标记...但是当我使用IF检查两个LatLng值是否相同时,程序不会执行IF声明......目的地标记没有反弹..我的编码是

        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);
       }
    }); 
Run Code Online (Sandbox Code Playgroud)

Kri*_*ris 21

在LatLng中尝试equals方法来比较2位置

改变 if(latlng1==latlng2)if(latlng1.equals(latlng2))


geo*_*zip 6

要确定2个google.maps.LatLng对象是否位于同一位置,请选择一个距离(如0.1米),计算它们之间的距离,如果它小于阈值,则假设它们是相同的位置.如果对象只是相同的对象,则只返回true.比较2个浮点数是有问题的,并且比较两对浮点对象更是如此.

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

确保包含几何库