0

안녕하세요 좌표 배열에서 제공되는 WatchPosition() 함수에 특정 Lat/Lon 함수를 제공하는 방법이 있는지 궁금합니다.WatchPosition() 함수를 사용하는 방법 다른 좌표계를보기위한 Geolocation

기본 좌표와 같은 것을 사용하는 콜백 함수를 만드는 데 문제가 있습니다. lat coords. long

다른 말로하면 비슷한 방식으로 사용할 수있는 일종의 함수를 만들려고하지만 임의의 좌표 세트를 제공하여 watchPosition()이 루프를 통해 배열을 업데이트하고 다른 위치가 설정됩니다.

답변

2

내가 같은 일을 할 필요가

if(navigator.geolocation) { 

    navigator.geolocation.watchPosition(function(position) { 
     var pos = new google.maps.LatLng(position.coords.latitude, 
             position.coords.longitude); 

은} 그래서 모의 하나로 navigator.geolocation 객체를 대체 할 테스트 스크립트를 썼다. 여기 내 코드는 다음과 같습니다

// Replace real geolocation with mock geolocation 
delete navigator.geolocation;  
navigator.geolocation = { 
    isMock: true, 
    paused: true, 
    delay: 100, 
    shouldFail: false, 
    failsAt: -1, 
    unFailsAt: -1, 
    errorMessage: "There was an error retrieving the position!", 
    currentTimeout: -1, 
    lastPosReturned: 0, 
    overrideAccuracy: 0, // accuracy in m to return for all positions (overrides any existing accuracies defined in the waypoints) 
    useOverrideAccuracy: false, // Whether to override acurracies defined in the waypoints with the above override accuracy  

    _geoCall: function(method, success, error, repeat) { 

     return this.currentTimeout = window[method].call(null, __bind(function() { 

     var nextPos; 

     if(!this.paused && this.lastPosReturned < this.waypoints.length - 1){ 
      nextPos = this.lastPosReturned++;    
     }else{ 
      this.lastPosReturned = nextPos = 0; 
     } 


     if(!this.shouldFail && nextPos == this.failsAt) this.shouldFail = true; 
     if(this.shouldFail && nextPos == this.unFailsAt) this.shouldFail = false; 

     if(repeat) this._geoCall("setTimeout", success, error, true); 

     if (this.shouldFail && (error != null)) {    
      return error(this.errorMessage); 
     } 
     else if (success != null && !this.paused) {     
      var result = this.waypoints[nextPos]; 
      result.isMock = true; 
      if(this.useOverrideAccuracy) result.coords.accuracy = this.overrideAccuracy;    
      success(result); 
      return nextPos; 
     } 
     }, this), this.delay); 

    }, 
    getCurrentPosition: function(success, error) { 
    return this._geoCall("setTimeout", success, error, false); 
    }, 
    watchPosition: function(success, error) { 
    this._geoCall("setTimeout", success, error, true); 
    return this.currentTimeout; 
    }, 
    clearWatch: function(id) { 
    return clearInterval(this.currentTimeout); 
    }, 
    waypoints: [] 
}; 

그럼 난 그냥 위치 물체로 navigator.geolocation.waypoints 배열을 채울 필요가있다.

+0

watchPosition을 사용하여 자신의 위치를 ​​추적 한 다음 동일한 기능을 사용하여 다른 사람의 위치를 ​​보려고했는데 배열에서 좌표가 제공 되었기 때문입니다. – Kayoti

+0

"실제"위치 업데이트와 모의 위치 업데이트를 동시에 받고 싶습니까? 위 코드를 모의 위치 업데이트에 사용할 수 있지만 navigator.mockGeolocation으로 이름을 바꿉니다. 그런 다음 실제 위치 (navigator.geolocation.watchPosition)와 모의 위치 (navigator.mockGeolocation.watchPosition)를 동시에 볼 수 있습니다. – DaveAlden

+0

고맙습니다 – Kayoti

관련 문제