2014-09-11 3 views
0

에 맞게 조정했습니다.이 문제를 며칠 동안 해결하려고 노력했지만 제한된 javascript 지식으로 인해 실패하고있는 것 같습니다.getCurrentPosition을 서버 쪽 asp.net 페이지

시나리오 : 나는 그것에 버튼 (비주얼 스튜디오 2010 (VB))와 함께 ASP.Net 페이지를 가지고
내가 jQuery를 버튼 클릭으로 jQuery를
을 사용하고 내 영문 페이지
, 현재 위치를 로컬 저장소에 저장하려고합니다.
온라인 상태 인 경우 서버 쪽 단추를 클릭하여 코드 숨김 페이지로 전달하려고합니다.
오프라인 인 경우 아무 것도하지 않거나 저장하지 않은 위치를 저장하려고하지만 서버 측 버튼이 실행되지 않도록하고 싶습니다.
다음 번에 버튼을 클릭 할 때 온라인 상태 인 경우 서버 측 버튼을 클릭하고 로컬 저장소의 원래 위치를 코드 숨김으로 전달하려고합니다.

나는 수많은 방법을 시도했지만 navigator.geolocation.getCurrentPosition의 '실행 순서'로 인해 문제가 발생했습니다.

문제는 getCurrentPosition '화재' 내 코드의 함수 매개 변수가 내 asp.net 숨겨진 컨트롤에 저장된 위치를 가져온 거에요 지점에 도달 한 것입니다! 이 시점에서 로컬 저장소는 비어 있습니다. 내 코드가 실행 된 후 로컬 저장소가 채워 집니까?

희망은 나 자신을 설명했습니다!

모든 머리카락을 찢기 전에 도움을 받으실 수 있습니다!

은 aspx 페이지에 코드를 아래로 스트라이프 :
(코드 뒤에 페이지가 btnLocation라는 버튼이있다 (유선 클릭 이벤트를 가지) 아래 참조) aspx.vb에서 코드 숨김

<script language="javascript" type="text/javascript"> 
    $(document).ready(function() { 
     $("#btnLocation").click(function() { 
      var savedLocation = localStorage.getItem('pos'); 
      if (savedLocation == '') { 
       if (navigator.geolocation) { 
        navigator.geolocation.getCurrentPosition(function (pos) { 
         var myPos = pos.coords.latitude + "," + pos.coords.longitude; 
         localStorage.setItem('pos', myPos); 
        }, function() { 
         alert("Unable to find your location"); 
        }); 
       } else { 
        alert("Browser does not support GeoLocation! "); 
       } 
      } 
      if (navigator.onLine == true) { 
       $('#hdnLocation').val(localStorage.getItem('pos')); 
       localStorage.removeItem('pos'); 
       return true; 
      } else { 
       return false; 
      } 
     }); 
    }); 
</script> 

:

Protected Sub btnClick(sender As Object, e As System.EventArgs) Handles btnLocation.Click 
    Dim ArrivedLocation As String = hdnLocation.Value.ToString.Trim 
End Sub 

답변

0

관심있는 사람이라면 누구나 '해결 방법'으로이 문제를 해결할 수 있습니다. 더 나은 방법이있을 것이라고 확신하지만 ...

내 aspx 페이지 (btnLocation)에 숨겨진 버튼을 추가했습니다.

jQuery를 :

<script language="javascript" type="text/javascript"> 
    $(document).ready(function() { 
      // Class for storing location... 
      function ourLocation(theLongitude, theLatitude, theMessage) { 
       this.longitude = theLongitude, 
       this.latitude = theLatitude, 
       this.message = theMessage 
      }; 

      // Main button click... 
      $("#btnArrived").click(function() { 
       if (navigator.geolocation) { 
        navigator.geolocation.getCurrentPosition(xLocation); 
       } 
       return false 
      }); 

      // Function that returns coordinates from getCurrentPosition... 
      function xLocation(position) { 
       var myLocation = new ourLocation('', '', ''); 
       var storedNameForLocation = 'arrived_location_' + jobNo + '_' + trade; 
       myPos = position; 
       myLocation.latitude = myPos.coords.latitude; 
       myLocation.longitude = myPos.coords.longitude; 
       myLocation.message = getCurrentTime(); 
       localStorage.setItem(storedNameForLocation, JSON.stringify(myLocation)); 
       $('#btnLocation').click(); 
      } 

      // Hidden button click (that fires server-side or not)... 
      $("#btnLocation").click(function() { 
       if (navigator.onLine == true) { 
        var storedNameForLocation = 'arrived_location_' + jobNo + '_' + trade; 
        $('#hdnLocation').val(localStorage.getItem(storedNameForLocation)); 
        return true; 
       } else { 
        return false; 
       } 
      }); 

    }); 
</script> 

및 서버 측 :

Protected Sub btnClick2(sender As Object, e As System.EventArgs) Handles btnLocation.Click 
    Dim ArrivedLocation As String = hdnLocation.Value.ToString.Trim 
End Sub 

은 적어도 그것을 작동합니다!

테드.

관련 문제