0

Hello I was wondering if there is a way to feed WatchPosition() function specific Lat/Lon that are supplied from an array of coordinates.

I am having trouble creating a callback function that utilizes something like the default coords. lat coords. long

in other words I want to create some sort of a function that can be used in a similar fashion but supplies my arbitrary set of coordinates so that watchPosition() can update the location as the loop goes through the array and a different location is set.

if(navigator.geolocation) {

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

}

Kayoti
  • 49
  • 2
  • 9

1 Answers1

2

I needed to do the same thing so I wrote a test script to replace the navigator.geolocation object with a mock one. Here's my code:

// 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: []
};

Then I just need to populate the navigator.geolocation.waypoints array with position objects.

DaveAlden
  • 30,083
  • 11
  • 93
  • 155
  • I wanted to track my own position using watchPosition and then watch someone else position using the same function were their the coordinated are supplied from an array. – Kayoti Jul 10 '13 at 19:13
  • So you want to receive both "real" position updates and mock position updates simultaneously? You could use my code above for mock position updates, but rename it to navigator.mockGeolocation. Then you will be able to watch both the real position (navigator.geolocation.watchPosition) and the mock position (navigator.mockGeolocation.watchPosition) at the same time. – DaveAlden Jul 10 '13 at 22:37
  • Cool, glad you got it to work :-) Please accept my answer to this question so other people know it has been answered :-) – DaveAlden Jul 14 '13 at 14:12
  • click on the "tick" below the up/down arrows near the top-left of this answer. I goes green when you hover the mouse over it. You could click the up arrow too ;-) – DaveAlden Jul 15 '13 at 08:30