2013-03-13 69 views
5

我使用DirectionsRenderer開發了一個包含路線的地圖......如果起點和目的地在同一地點,我需要將目的地標記反彈......但是當我檢查兩個LatLng值是否與IF相同時,程序會執行不執行IF語句...目的地標記沒有被退回..我的編碼是如何使用google map api v3中的if條件檢查兩個LatLng值?

 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); 
     } 
    }); 

回答

20

Try equals method in LatLng to compare 2 location

變化if(latlng1==latlng2)if(latlng1.equals(latlng2))

+0

但是看,如果你想通過位置附近檢查您可以使用@ geocodezip的解決方案 – Kris 2013-03-13 06:36:07

+0

雅肯定朋友...顯然是一個精心設計的解決方案...將在未來使用geocodezip的代碼非常精確地映射api ...感謝您的支持rt – Ree 2013-03-13 06:43:55

6

要確定2個google.maps.LatLng物件是相同的位置,pi距離(如0.1米),計算它們之間的距離,如果它小於閾值,則假定它們是相同的地方。比較對象只會返回true,如果它們是相同的相同對象。比較兩個浮點數是有問題的,並且比較兩對浮點對象更是如此。

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

一定要包括geometry library

+0

urs也工作先生...但Krisl代碼是一個更簡單的...但感謝您的支持...期待我的未來的問題也一樣... – Ree 2013-03-13 06:36:20

+0

+1 @geocodezip更好的解決方案,以防萬一檢查附近的位置。謝謝 – Kris 2013-03-13 06:37:32

相關問題