Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在谷歌地图api v3中使用if条件检查两个LatLng值?_Javascript_Google Maps_Google Maps Api 3_If Statement - Fatal编程技术网

Javascript 如何在谷歌地图api v3中使用if条件检查两个LatLng值?

Javascript 如何在谷歌地图api v3中使用if条件检查两个LatLng值?,javascript,google-maps,google-maps-api-3,if-statement,Javascript,Google Maps,Google Maps Api 3,If Statement,我已经开发了一个使用DirectionsRenderer的路线地图。。。我需要的目的地标记反弹,如果起点和目的地在同一个地方。。。但当我使用IF检查两个LatLng值是否相同时,程序不会执行IF语句。。。目标标记未被反弹。。我的编码是 var myOptions = { center: new google.maps.LatLng(default_latitude,default_longitude), zoom: 4,

我已经开发了一个使用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);
       }
    }); 


if(latlng1==latlng2)
更改为
if(latlng1.equals(latlng2))
要确定两个google.maps.LatLng对象是否位于同一位置,请选择一个距离(如0.1米),计算它们之间的距离,如果该距离小于阈值,则假定它们位于同一位置。仅当对象是相同的对象时,比较对象才会返回true。比较两个浮点数是有问题的,而比较两对浮点数对象更是如此

var SameThreshold = 0.1;
if (google.maps.geometry.spherical.computeDistanceBetween(latlng1,latlng2) < SameThreshold)
   marker1.setAnimation(google.maps.Animation.BOUNCE); 
var SameThreshold=0.1;
if(google.maps.geometry.spherical.ComputedDistanceBeween(latlng1,latlng2)
一定要包括


但是你看,如果你想在附近的位置查看,你可以使用@geocodezip的解决方案你当然是朋友。。。tats显然是一个微调的解决方案。。。将使用geocodezip的代码在未来非常小的地图api。。。谢谢你的两位支持者也在工作,先生。。。但是Krisl代码更简单。。。但是谢谢你的支持。。。我未来的问题也会如此…+1@geocodezip,以便更好地解决问题,以防查看附近的位置。非常感谢。