5

我已经使用 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);
       }
    }); 
4

2 回答 2

21

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

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

于 2013-03-13T05:10:47.227 回答
6

要确定 2 个 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); 

一定要包括几何库

于 2013-03-13T05:31:15.717 回答