2013-11-29 67 views
0

我已經寫了一個函數來查找用戶的位置並相應地更改鏈接。功能如下:爲什麼我的腳本不能正常工作?

/* 
This function is to determine the users location and integrate that data into the navigation aspect 
*/ 
function findLocation() { 
    try { // try to find the users location and then manipulate it to get the correct google maps query strings/links 
     var destinations = { // destinations to go to: to be added to the query string 
      bridgeFromUs: "Ambassador+Bridge,+Detroit,+MI", 
      bridgeFromCa: "Ambassador+Bridge,+Ambassador Bridge,+Windsor,+ON,+Canada", 
      tunnelFromUs: "Detroit+Windsor+Tunnel,+Detroit,+MI", 
      tunnelFromCa: "The+Windsor+Detroit+Tunnel,+Windsor,+ON,+Canada" 
     }; 
     // the following variables are the correct variable for the query string 
     var bridgeAddress; 
     var tunnelAddress; 
     // switch through the values for the country variable -- it is declared in the head of each HTML page 
     switch (country) { 
     case "CA": // if it is for Canada, reflect that in the query strings 
      bridgeAddress = destinations.bridgeFromCa; 
      tunnelAddress = destinations.tunnelFromCa; 
      break; 
     case "US": // if it is for the US, reflect that in the query strings 
      bridgeAddress = destinations.bridgeFromUs; 
      tunnelAddress = destinations.tunnelFromUs; 
     } 
     // now the preliminary work is done -- let's find out where they are 
     navigator.geolocation.getCurrentPosition(
      function (position) { 
       var latitude = position.coords.latitude; 
       var longitude = position.coords.longitude; 
       // store the position for the session -- VERY unnecessary, but it is cool and HTML5! ;) 
       sessionStorage.setItem('latitude', latitude); 
       sessionStorage.setItem('longitude', longitude); 
       // link for where they are 
       var addr = "https://maps.google.com/maps?f=d&source=s_d&saddr=" + latitude + ",+" + longitude + "&daddr="; 
       console.log(addr); 
       return addr; 
      }, 
      function createLink(addr) { 
       var baseLink = arguments[0]; 
       // final links 
       var link = { 
        bridge: baseLink + bridgeAddress, 
        tunnel: baseLink + tunnelAddress 
       }; 
       // set them in the DOM 
       document.getElementById('bridgeLink').href = link.bridge; 
       document.getElementById('tunnelLink').href = link.tunnel; 
      } 
     ); 
     // if we fail... 
    } catch (e) { 
     // send an alert... 
     alert("Hey! We can't find out where you are! You should check back later."); 
     // ...and set some links anyway. 
     var lin = "http://maps.google.com"; 
     document.getElementById('bridgeLink').href = lin; 
     document.getElementById('tunnelLink').href = lin; 
    } 
} 

我知道功能正在運行,因爲它是console.log荷蘭國際集團的addr變量就好了。但它不會改變鏈接的地址。該鏈接仍然只設置爲我設置的默認"#"。我試過用控制檯進行調試,但沒有顯示任何錯誤。這是怎麼回事?

這是解決問題的鏈接:http://54.201.70.251/static/canada.html

+0

您似乎在將'createLink'函數作爲(second)'error'參數傳遞給'navigator.geolocation.getCurrentPosition'。可能不是你想要的 – Phil

+0

@Phil謝謝。我會解決這個問題 – Tjs

回答

1

建立連結應該只在函數(位置)被調用,現在你通過建立連結的錯誤回調。

navigator.geolocation.getCurrentPosition(success, error