2013-05-17 23 views
11

我有一個關於顯示箭頭到位置的問題。 這是我擁有的: enter image description here不正確的結果箭頭(角度)到地理定位

您可以將當前位置保存在本地存儲中。一段時間後,當你進一步例如30米時,你可以點擊第二個按鈕「顯示方向到上一個位置!」將箭頭指向您之前的位置。這是一個手機網站,所以不是原生應用程序。

這裏是我的代碼:

<!DOCTYPE html> 
<html> 
    <head> 
     <!-- JQUERY SCRIPT AND COMPASS SCRIPT AND MODERNIZR SCRIPT --> 
     <script src="http://code.jquery.com/jquery-1.8.3.js"></script>  
    </head> 
    <body> 
     <div class="container"> 
      <h1>Find your location</h1> 
      <div class="row"> 
       <div class="span12"> 
        <!-- Save your current location --> 
        <button class="grey" id="btnFindLocation">Save my current location!</button> <br> 
        <!-- Show direction to previous location --> 
        <button class="grey" id="btnShowDirection">Show direction to previous location!</button> <br><br> 

        <!-- Arrow in direction to location --> 
        <img id="myarrow" class="deviceorientation" src="http://nielsvroman.be/tapcrowd/arrow.png" /> 
      </div> 
     </div> 
     <script> 
     $(window).ready(function(){ 
      // orientation object to save heading of the device 
      var orientation = {}; 
      /* Find location button */ 
      $("#btnFindLocation").click(findLocation); 
      /* Show direction button */ 
      $("#btnShowDirection").click(showDirection); 

      // Device orientation 
      if (window.DeviceOrientationEvent) { 
       window.addEventListener("deviceorientation", handleOrientation, false); 
      } 
      else{ 
       alert("Device Orientation is not available"); 
      } 

      function handleOrientation(orientData) 
      { 
       var alpha = orientData.alpha; 

       // To get the compass heading, one would simply subtract alpha from 360 degrees. 
       var heading = 360 - alpha; 
       orientation.value = heading; 

      } 

      function findLocation(){ 
       // Check if geolocation is supported in browser 
       if (navigator.geolocation) 
       { 
        // Succes function: geoSucces  Error function: geoError 
        navigator.geolocation.getCurrentPosition(geoSucces,geoError); 
       } 
       else 
       { 
        alert("Geolocation is not supported!"); 
       } 
      } 

      function geoSucces(position) 
      { 
       // Check if localstorage is supported in browser 
       if (Modernizr.localstorage) 
       { 
        // Object declaration in localStorage 
        localStorage.setItem('position', '{}'); 
        // Save position object in localstorage 
        localStorage.setItem('position', JSON.stringify(position)); 
       } 
       else 
       { 
        alert("localStorage is not available!"); 
       } 
      } 

      var watchProcess = null; 
      function showDirection(){ 
       if (navigator.geolocation) 
       { 
        if (watchProcess == null) { 
         // Succes function: geoWatchSucces  Error function: geoError 
         navigator.geolocation.watchPosition(geoWatchSucces,geoError); 
        } 
       } 
       else 
       { 
        alert("Geolocation is not supported!"); 
       } 
      } 

      function geoWatchSucces(position) 
      { 
       // Check if localStorage is supported in browser 
       if (Modernizr.localstorage) 
       { 
        // Get previous location out of localstorage 
        var location = JSON.parse(localStorage.getItem('position')); 
       } 
       else 
       { 
        alert("localStorage is not available!"); 
       } 
       // lat/lon of location in localstorage and current location 
       var lat1 = location.coords.latitude; 
       var lon1 = location.coords.longitude; 
       var lat2 = position.coords.latitude; 
       var lon2 = position.coords.longitude; 

       // angle to location 
       var angle = Math.atan2(lon2 - lon1, lat2 - lat1); 

       // degrees device 
       var degrees = orientation.value; 

       // degrees of device - angle 
       var result = degrees - angle; 

       // Set arrow to direction location 
       setArrowRotation(result); 
      } 

      // Stop monitoring location 
      function stopShowDirection() { 
       if (navigator.geolocation) 
       { 
        if (watchProcess != null) 
        { 
         navigator.geolocation.clearWatch(watchProcess); 
         watchProcess = null; 
        } 
       } 
       else 
       { 
        alert("Geolocation is not supported!"); 
       } 
      } 


      // Error function geolocation 
      function geoError(error) 
      { 
       switch(error.code) 
       { 
        case error.PERMISSION_DENIED: alert("user did not share geolocation data"); 
        break; 
        case error.POSITION_UNAVAILABLE: alert("could not detect current position"); 
        break; 
        case error.TIMEOUT: alert("retrieving position timed out"); 
        break; 
        default: alert("unknown error"); 
        break; 
       } 
      } 

      // Functions to set direction arrow 
      function getsupportedprop(proparray){ 
       var root=document.documentElement; 
       for (var i=0; i<proparray.length; i++){ 
        if (proparray[i] in root.style){ 
         return proparray[i]; 
        } 
       } 
       return false; 
      } 

      var cssTransform; 
      function setArrowRotation(x){ 
       if(cssTransform===undefined){ 
        cssTransform=getsupportedprop(['transform','webkitTransform','MozTransform','OTransform','msTransform']); 
       } 
       if(cssTransform){ 
        document.getElementById('myarrow').style[cssTransform]='rotate('+x+'deg)'; 
       } 
      } 
     }); // END OF DOCUMENT READY 

     </script> 
    </body> 
</html> 

我做些什麼來設置上位置的方向的箭頭是: - 呼叫watchprocess功能 - 先前的位置+ /緯度獲取緯度/經度的當前位置 - 計算與之前的位置相關的角度 - 檢查移動設備的度數 - 我使用deviceorientation事件執行此操作,我讀取設備的標題= 360 - alpha(來源:http://dev.w3.org/geo/api/spec-source-orientation.html#introduction) - The最終角度是移動設備的程度 - 以前s計算出的角度 - 以該角度設置箭頭

但是,我總是得到奇怪的結果... 當我以前的位置是進一步10米箭頭是不正確的大部分時間。

有誰知道我爲什麼得到這個結果?

這裏是一個的jsfiddle:jsfiddle

提前感謝! Niels

+0

可能重複的[顯示箭頭(角度),以考慮到裝置的航向的位置](http://stackoverflow.com/questions/16542774/show-arrow-angle-to-a-location-考慮到設備的標題) –

回答

2

您是否嘗試過設置精度?

navigator.geolocation.getCurrentPosition(geoSucces,geoError, {enableHighAccuracy: true}); 
+0

是的。這是我得到的準確性結果: 當我點擊「保存我的位置」時,我的準確度爲43.612 ....當我點擊「顯示方向到上一個位置」時,我的準確度爲20 ....這個問題? – nielsv

1

檢查您的測試設備上position.coords.accuracy是否足夠低。你可能會遇到非常不準確的經緯度結果。

+0

當我點擊「保存我的位置」時,我的準確度爲43.612 .... 當我點擊「顯示方向到上一個位置」時,我的準確度爲20 ....這是問題嗎? – nielsv

+0

你可以檢查,相當容易。在地圖上繪製2點,然後繪製一個半徑爲準確度的圓。您計算的角度可以是您兩個圓圈中任意兩點之間的任意角度。 – Tyron