transistorsoft / cordova-background-geolocation-SampleApp

Cordova Background Geolocation Sample Application
https://www.transistorsoft.com/shop/products/cordova-background-geolocation
MIT License
122 stars 78 forks source link

Very large GPS position variation #80

Open danielaraujo opened 7 years ago

danielaraujo commented 7 years ago

Good Morning, I'm using in my app the cordova-plugin-googlemaps along with background-geolocation. When I try to use background-geolocation positioning to get a route next to the google API, it often has a very large variation and ends up tracing the route on the way to the opposite side. Using the sample it always stays inside the track where I'm walking. My configuration looks like this:

_backgroundGeolocationInstance.configure ({
// Geolocation config
DesiredAccuracy: 0,
DistanceFilter: 0,
                DisableElasticity: true,
                GeofenceProximityRadius: 1000,
                LocationTimeout: 60,
                LocationUpdateInterval: 1000,
FastestLocationUpdateInterval: 1000,
Defer Time: 0,

                // Activity Recognition config
                StopTimeout: 5,
                ActivityType: 'AutomotiveNavigation',
ActivityRecognitionInterval: 1000,
TriggerActivities: 'in_vehicle',
                
                StopOnTerminate: true,
StartOnBoot: true,
                HeartbeatInterval: 60,
                ForegroundService: true,
StationaryRadius: 20,
// Application config
Debug: false
}

Is there anything I can do to improve accuracy and it only returns me to the position within the path I'm actually in at the moment?

christocracy commented 7 years ago

Well, for one thing, all your config params are incorrectly capitalized. Your entire config is essentially invalid.

eg: DesiredAccuracy != desiredAccuracy.

Where did you get the idea to do that?

Defer Time?? You can't have spaces in a JSON key without "". That should be generating a syntax error.

danielaraujo commented 7 years ago

Excuse. I am from Brazil and these errors pointed out because of Google Translate.

_backgroundGeolocationInstance.configure({
                // Geolocation config
                desiredAccuracy: 0,
                distanceFilter: 0,
                disableElasticity : true,               
                geofenceProximityRadius : 1000,
                locationTimeout : 60,
                locationUpdateInterval: 1000,
                fastestLocationUpdateInterval: 1000,                
                deferTime: 0,                

                // Activity Recognition config
                stopTimeout : 5,
                activityType: 'AutomotiveNavigation',                
                activityRecognitionInterval: 1000,
                triggerActivities:'in_vehicle',

                stopOnTerminate: true,
                startOnBoot: true,
                heartbeatInterval : 60,
                foregroundService: true,
                stationaryRadius: 20,

                //desiredOdometerAccuracy: 10,

                // Application config               
                debug: false

            }
christocracy commented 7 years ago

If the plugin is working fine without cordova-plugin-googlemaps, the problem must be with cordova-plugin-googlemaps

danielaraujo commented 7 years ago

Actually my problem is with the variation of the Lat and Lng I get. Even stopped.

Anyway, thanks for the reply. I'll look elsewhere.

xhava commented 7 years ago

I have the same problem and no solution, I have some variations on my ponga I think this happens when the cell phone has a low signal and it does not know the correctly position

xhava commented 7 years ago

@danielaraujo do you find a solution to reduce the variations on your gps points

christocracy commented 7 years ago

The only thing you can do is set desiredAccuracy: 0. Everything else depends upon device settings and network environment.

xhava commented 7 years ago

@christocrazy do u know some algorithm to apply, as you know sometimes the gps fails and register bad location. I would know if you know some solution to remove or detect which of that point are unusual on my tracking path

christocracy commented 7 years ago

This s the purpose of location attribute accuracy

christocracy commented 7 years ago

Any location problems are due to device and/or network environment.

Here's a ~300km round trip with my Google Pixel last weekend: not a single poor location.

arapocket commented 7 years ago

I created an algorithm to solve this issue -- I might need to tweak the maximum accepted delta but here is what I did:

  1. I modified onLocation() like so:

    onLocation(location: any, taskId: any) {
    
    if (this.lastLocation != null) {
      if (this.locationIsAccurate(location, this.lastLocation)) {
        this.continueOnLocation(location, taskId);
      }
    }
    else {
      this.continueOnLocation(location, taskId);
    }
    }
  2. I moved everything that used to be in onLocation() to continueOnLocation()

    continueOnLocation(location: any, taskId: any) {
    console.log('[js] location: ', location);
    let bgGeo = this.bgService.getPlugin();
    this.setCenter(location);
    if (!location.sample) {
      this.zone.run(() => {
        // Convert meters -> km -> round nearest hundredth -> fix float xxx.x
        this.state.odometer = parseFloat((Math.round((location.odometer / 1000) * 10) / 10).toString()).toFixed(1);
      });
    }
    bgGeo.finish(taskId);
    }
  3. I created the locationIsAccurate() method.

    locationIsAccurate(location: any, lastLocation: any) {
    
    let maxDelta = 0.0001;
    
    let deltaLatitude = Math.abs(
      (location.coords.latitude - this.lastLocation.coords.latitude)
    )
    
    let deltaLongitude = Math.abs(
      (location.coords.longitude - this.lastLocation.coords.longitude)
    )
    
    //CHECK IF DISTANCE IS TOO FAR, ie PREVENT WEIRD LINES ON MAP
    
    console.log("Comparing this location and last location:");
    console.log("Current Latitude: " + location.coords.latitude);
    console.log("Current Longitude: " + location.coords.longitude)
    console.log("Last Latitude: " + this.lastLocation.coords.latitude);
    console.log("Last Longitude: " + this.lastLocation.coords.longitude);
    console.log("Delta Latitude: " + deltaLatitude);
    console.log("Delta Longitude: " + deltaLongitude);
    
    if (
      (
        (deltaLatitude < maxDelta) &&
        (deltaLongitude < maxDelta))
    ) {
      return true;
    } else {
      console.log("Location is not accurate: ignoring coordinate");
      this.polyline.getPath().pop();
      return false;
    }
    }
christocracy commented 7 years ago

Why are you not simply checking the location accuracy? "Weird" locations likely have a very low accuracy (e.g. > 1000)

arapocket commented 7 years ago

I always have my accuracy on -1 when I encounter the issue.

christocracy commented 7 years ago

I'm not talking about desiredAccuracy, I said location accuracy.

xhava commented 7 years ago

my accuracy it is always in 3.0 to 4.0 captura de pantalla 2017-09-05 a la s 14 05 24

christocracy commented 7 years ago

There is nothing to complain about with locations having accuracy 3-5 meters. Those are perfectly fine locations

arapocket commented 7 years ago

Your plugin is amazing and I would give up entirely on using Ionic if it didn't exist. So thank you. I was just sharing my solution.

christocracy commented 7 years ago

It's available for React Native and pure native apps too.

xhava commented 7 years ago

so i don't know what is wrong or what is happening

my points look like this, all in red are bad locations i solve this a litle bit adding a request to api of google snap to roads

captura de pantalla 2017-09-03 a la s 21 58 21

christocracy commented 7 years ago

And what is the corresponding location accuracy for "locations in red"? There's no way it's 3-5 meters.

There's nothing to be done in the plugin to solve that. It's due to the device and/or network environment. It could be power-saving mode on the device being engaged.

xhava commented 7 years ago

thanks @christocracy ur plugin is perfect i just wanted to know if there was something to do for fix that.

christocracy commented 7 years ago
xhava commented 7 years ago

my acurracy form some points in red is to accuracy";d:16.699999999999999.

so the value of accurracy means that if the value is high like this accuracy";d:16.699999999999999 i could consider it as a bad point.

christocracy commented 7 years ago

accuracy > 10 typically means the location did not come from GPS; it came from Wifi instead.

Personally, I wouldn't call a location with accuracy 17 bad. I'd call a location with accuracy > 200 "bad"

arapocket commented 7 years ago

@christocracy I'm building something that will be used indoors a lot. So instead of comparing the actual latitude and longitude, I see now I could've just checked that the accuracy is within 5 meters before adding a breadcrumb. Oh well, I guess it's the same thing.

christocracy commented 7 years ago

Indoors, the best accuracy you'll get is 40 meters. GPS is impossible indoors.

xhava commented 7 years ago

thanks @christocracy for your help.

arapocket commented 7 years ago

I mean it still depends on what type of roof you're under, right? It seems to work fine for me in most buildings.

christocracy commented 7 years ago

GPS receiver on mobile devices is a microwave radio receiver. It doesn't take much to block a low amplitude / high frequency signal from space.

Put a GPS Test app on your device an see for yourself how many satellites your device "locks-on to".

christocracy commented 7 years ago

Maybe if you're under a grass roof, you might get a signal. Concrete or metal? forget it.

arapocket commented 7 years ago

To be fair, I tried it in a very hipster building in LA that I work at. The roof might be made of recycled cereal boxes...

christocracy commented 7 years ago

Being next to windows will help with "seeing" GPS satellites.

richrichDev commented 2 years ago

to solve this issue , in ionic if you are using javascript google maps API in console.cloud.google.com, after you get the locations use Snap to roads API in console.cloud.google.com to draw the real path and then you will get the real path on the route and you dont get points outside the path like you had. but Still you have a problem that : if someone is running in a football field let's say , if you use snap to roads API the path will be drawn on the routes outside not inside the field , i dont know if there is a solution for this ...