siege-nnn commented on issue #115: Frequency/Interval feature for 
`watchPosition`
URL: 
https://github.com/apache/cordova-plugin-geolocation/issues/115#issuecomment-427805007
 
 
   @luisfernando-rf apologies for taking time to respond. I had to remove quite 
a bit of my other logic that I had in place and is not relevant. Let me know if 
you need assistance with the below, hopefully it would be self explanatory.
   
   //declare before constructor
     locationLastUpdateTime = null
     locationMinUpdateFrequency = 5 * 1000 //~5s between user location updates
     private watchLocation;
     myPositionMarker = null;
   
     toggleLocationTrace() {
   
         try {
           this.watchLocation = this.geolocation.watchPosition(options)
             .filter((p) => p.coords !== undefined) //Filter Out Errors
             .subscribe(position => {
               console.log('position::', position, 'L:', 
position.coords.longitude + ' ' + position.coords.latitude);
   
               //only continue if its time to check
               let now = new Date();
               let duration: number
               try {
                 duration = now.getTime() - 
this.locationLastUpdateTime.getTime()
               } catch (error) {
                 duration = 999999999
               }
               if (this.locationLastUpdateTime == null || duration >= 
this.locationMinUpdateFrequency) {
                 console.log('update is needed.::', duration)
   
                     let newPosition = { lat: position.coords.latitude, lng: 
position.coords.longitude }
                     if (this.myPositionMarker !== null) {
                       this.myPositionMarker.setPosition(newPosition);
   
                       if (this.followUserPosition == true) {
                         this.map.moveCamera({
                           target: newPosition,
                           //'zoom': 18,
                           //'bearing': 140
                         });
                       }
                     } else {
                       //create the marker
                       this.map.addMarker(
                         {
                           icon: 'red',
                           title: 'me',
                           //animation: google.maps.Animation.DROP,
                           draggable: false,
                           position: newPosition
                         })
                         .then(marker => {
                           this.myPositionMarker = marker
                         console.log('my marker::', this.myPositionMarker)
                         });
                     }
             
                 this.locationLastUpdateTime = now;
               } else {
                 //console.log('ignore position update::', duration);
               }
   
   
             });
         } catch (error) {
           console.log('unable to watch current location::', error)
         }
   }

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to