# How much is my speed?

**URL:** <https://forum.ionicframework.com/t/how-much-is-my-speed/99185>\
**Category:** ionic-v3\
**Created:** [July 22, 2017, 3:54pm UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185 "2017-07-22T15:54:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsa](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@rsa](https://forum.ionicframework.com/u/rsa)\
**Post date:** [July 22, 2017, 3:54pm UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/1 "2017-07-22T15:54:59Z")

</div>

In fallowing code I’m trying to access new geolocation of current device and speed of motion but I don’t know am i did it right or no, the output is attached also bellow Ho much is my speed?

```auto

@Component({
  selector: 'page-page1',
  templateUrl: 'page1.html'
})
export class Page1 {
  public lat;
  public lng;
  public speed;
  public watch: any;
  constructor(public navCtrl: NavController, public Geolocation: Geolocation, public zone: NgZone,
  public backgroundGeolocation: BackgroundGeolocation,
  private deviceMotion: DeviceMotion
  ) {
this.startTracking();
  }
  startTracking() {
 this.deviceMotion.getCurrentAcceleration().then(
  (acceleration: DeviceMotionAccelerationData) => console.log(acceleration),
  (error: any) => console.log(error)
);
var subscription = this.deviceMotion.watchAcceleration().subscribe((acceleration: DeviceMotionAccelerationData) => {
  console.log(acceleration);
});

  let config = {
    desiredAccuracy: 0,
    stationaryRadius: 20,
    distanceFilter: 10, 
    debug: true,
    interval: 2000 
  };

  this.backgroundGeolocation.configure(config).subscribe((location) => {

    console.log('BackgroundGeolocation: ' + location.latitude + ',' + location.longitude);

    this.zone.run(() => {
      this.lat = location.latitude;
      this.lng = location.longitude;
      this.speed = location.speed;
    });

  }, (err) => {

    console.log(err);

  });

  this.backgroundGeolocation.start();

let options = {
  frequency: 3000, 
  enableHighAccuracy: true
};

this.watch = this.Geolocation.watchPosition(options).filter((p: any) => p.code === undefined).subscribe((position: Geoposition) => {

  console.log(position);

  this.zone.run(() => {
    this.lat = position.coords.latitude;
    this.lng = position.coords.longitude;
  });

});

}
stopTracking() {
  console.log('stopTracking');
  this.backgroundGeolocation.finish();
  this.watch.unsubscribe();

}
}

```

 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/9/9/990df8feb648c0851116103b15a375cabdbfe33a.png)

---

<div class="post-metadata">

**Author:** ![kvnjo](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@kvnjo](https://forum.ionicframework.com/u/kvnjo)\
**Post date:** [March 28, 2018, 1:26am UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/2 "2018-03-28T01:26:59Z")

</div>

is it work ? because i have tried it and nothing coming out…

---

<div class="post-metadata">

**Author:** ![rsa](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@rsa](https://forum.ionicframework.com/u/rsa)\
**Post date:** [March 28, 2018, 5:22pm UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/3 "2018-03-28T17:22:57Z")

</div>

At that time that was working for me. I used to check with car speed it was about same meter per secconds i think.

---

<div class="post-metadata">

**Author:** ![kvnjo](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@kvnjo](https://forum.ionicframework.com/u/kvnjo)\
**Post date:** [April 4, 2018, 12:55am UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/4 "2018-04-04T00:55:08Z")

</div>

Can i get the .TS file and the .html file ? because i had tried your code but it doesnt work on my laptop…  
Please send the .TS file and the .html file to my email : [jonathankevin88@gmail.com](mailto:jonathankevin88@gmail.com)

---

<div class="post-metadata">

**Author:** ![rsa](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@rsa](https://forum.ionicframework.com/u/rsa)\
**Post date:** [April 7, 2018, 5:40pm UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/5 "2018-04-07T17:40:26Z")

</div>

I don’t have them now but I used this link.

> **[Adding Background Geolocation to an Ionic 2 & 3 Application](https://www.joshmorony.com/adding-background-geolocation-to-an-ionic-2-application/)**
>
> In a previous tutorial, I have demonstrated how to use Geolocation in an Ionic 2 application. The method used in that tutorial is the “standard” way to use the Geolocation plugin, it wi…

---

<div class="post-metadata">

**Author:** ![kvnjo](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@kvnjo](https://forum.ionicframework.com/u/kvnjo)\
**Post date:** [April 11, 2018, 2:12am UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/6 "2018-04-11T02:12:36Z")

</div>

Hi…  
I had succeed used your code on my device…  
But the speed doesnt show on my device…(i will show it on my attach file

 ![Screenshot_20180411-090551](https://global.discourse-cdn.com/ionicframework/original/3X/5/8/58d9143ac33bf5bfc98bcc793263fe930cefd8c5.png))  
can u explain why ???  
And where do u try the code ?? at the car, motorcycle, or just walking ??

---

<div class="post-metadata">

**Author:** ![rsa](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@rsa](https://forum.ionicframework.com/u/rsa)\
**Post date:** [April 11, 2018, 3:47am UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/7 "2018-04-11T03:47:58Z")

</div>

I used the same code. speed is Meter / second and if you multiply ((speed \*3600)/1000) you will get the Km/Hrs this is my code

```auto
**HTML:**
<ion-header>
  <ion-navbar>
    <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button>
    <ion-title>Home</ion-title>
  </ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
  <ion-item>
    <ion-label color="primary">Latitued</ion-label>
    <ion-label color="danger">{{lat}}</ion-label>
  </ion-item>
  <ion-item>
<ion-label color="primary">Longitued</ion-label>
<ion-label color="danger">{{lng}}</ion-label>
  </ion-item>
  <ion-item>
    <ion-label color="primary" >Speed</ion-label>
    <ion-label color="secondery" >{{speed}}</ion-label>
  </ion-item>
</ion-list>
</ion-content>

```

`home.ts`

```auto
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { BackgroundGeolocation} from '@ionic-native/background-geolocation';
import { DeviceMotion, DeviceMotionAccelerationData } from '@ionic-native/device-motion';
import { NgZone } from '@angular/core';
import { Geolocation, Geoposition } from '@ionic-native/geolocation';
import 'rxjs/add/operator/filter';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  public lat;
  public lng;
  public speed;
  public watch: any;
  public x;
  constructor(public navCtrl: NavController, public Geolocation: Geolocation, public zone: NgZone,
    public backgroundGeolocation: BackgroundGeolocation,
    private deviceMotion: DeviceMotion
  ) {
    this.startTracking();
  }
  startTracking() {
    /*this.deviceMotion.getCurrentAcceleration().then(
      (acceleration: DeviceMotionAccelerationData) => //console.log(acceleration)
      this.x=0,
      (error: any) => console.log(error)
    );
    var subscription = this.deviceMotion.watchAcceleration().subscribe((acceleration: DeviceMotionAccelerationData) => {
      //console.log(acceleration);
    });
*/
    let config = {
      desiredAccuracy: 0,
      stationaryRadius: 5,
      distanceFilter: 10,
      debug: true,
      interval: 1000
    };

    this.backgroundGeolocation.configure(config).subscribe((location) => {

      //console.log('BackgroundGeolocation: ' + location.latitude + ',' + location.longitude);
// console.log(this.lat , this.lng, this.speed);

      this.zone.run(() => {
        this.lat = location.latitude;
        this.lng = location.longitude;
        this.speed = (location.speed * 3600)/1000 ; // can be speed * 3.6 and should be round for 2 decimal
        
      });

    }, (err) => {
      console.log(err);

    });

    this.backgroundGeolocation.start();

    let options = {
      frequency: 1000,
      enableHighAccuracy: true
    };

    this.watch = this.Geolocation.watchPosition(options).filter((p) => p.coords !== undefined)
    .subscribe((position: Geoposition) => {

      //console.log(position);

      this.zone.run(() => {
        this.lat = position.coords.latitude;
        this.lng = position.coords.longitude;
      });

    });

  }
  stopTracking() {
    console.log('stopTracking');
    this.backgroundGeolocation.finish();
    this.watch.unsubscribe();

  }
}

```

---

<div class="post-metadata">

**Author:** ![kvnjo](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@kvnjo](https://forum.ionicframework.com/u/kvnjo)\
**Post date:** [April 12, 2018, 2:18am UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/8 "2018-04-12T02:18:06Z")

</div>

Hi RSA…  
I had tried your code and its work on my device but there’s one problem…  
The speed just return Nan (Not a Number) ?  
 ![Screenshot_20180412-081256](https://global.discourse-cdn.com/ionicframework/original/3X/e/c/ec72dfd3aeaf60e0ff98e2f215f246e971827844.png)  
Do u know how to fix it ??

---

<div class="post-metadata">

**Author:** ![rsa](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@rsa](https://forum.ionicframework.com/u/rsa)\
**Post date:** [April 12, 2018, 6:28pm UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/9 "2018-04-12T18:28:04Z")

</div>

I don’t saw this before. it is because of multiplying and dividing on starting moment, i guess you can solve it by using `+ oprator`:

`this.speed = (+location.speed * 3600)/1000 ;`  
or  
`this.speed = Number.isNaN(location.speed * 3.6) ? 0 : (location.speed * 3.6);`

---

<div class="post-metadata">

**Author:** ![kvnjo](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@kvnjo](https://forum.ionicframework.com/u/kvnjo)\
**Post date:** [April 18, 2018, 2:50am UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/10 "2018-04-18T02:50:25Z")

</div>

Hi RSA,

Thanks a lot for your help. But the speed still not shown on my device. Even though i set the speed = location.speed, without the multiplication with 3.6. What should i do now…??  
This my app.ts script…

import { Component } from ‘@angular/core’;  
import { NavController } from ‘ionic-angular’;  
import { NgZone } from ‘@angular/core’;  
import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationResponse} from ‘@ionic-native/background-geolocation’;  
// // import { DeviceMotion, DeviceMotionAccelerationData } from ‘@ionic-native/device-motion’;  
import { Geolocation,Geoposition } from ‘@ionic-native/geolocation’;  
import ‘rxjs/add/operator/filter’

@Component({  
selector: ‘page-home’,  
templateUrl: ‘home.html’  
})  
export class HomePage {  
public lat;  
public lng;  
public speed: number;  
public watch: any;  
constructor(public navCtrl: NavController, public Geolocation: Geolocation, public zone: NgZone, private backgroundGeolocation: BackgroundGeolocation) {  
this.startTracking();  
}  
startTracking() {  
/\*this.deviceMotion.getCurrentAcceleration().then(  
(acceleration: DeviceMotionAccelerationData) =\> [//console.log](https://console.log)(acceleration)  
this.x=0,  
(error: any) =\> console.log(error)  
);  
var subscription = this.deviceMotion.watchAcceleration().subscribe((acceleration: DeviceMotionAccelerationData) =\> {  
[//console.log](https://console.log)(acceleration);  
});  
\*/  
const config: BackgroundGeolocationConfig = {  
desiredAccuracy: 0,  
stationaryRadius: 5,  
distanceFilter: 10,  
debug: true,  
interval: 1000  
};

```
  this.backgroundGeolocation.configure(config)
  .subscribe((location: BackgroundGeolocationResponse) => {

    //console.log('BackgroundGeolocation: ' + location.latitude + ',' + location.longitude);

```

// console.log(this.lat , this.lng, this.speed);  
console.log(“cekaja”);

```
      this.lat = location.latitude;
      this.lng = location.longitude;
      this.speed = location.speed; // can be speed * 3.6 and should be round for 2 decimal
      console.log("coba");
  
  });

  this.backgroundGeolocation.start();
  
  this.backgroundGeolocation.stop();
  
  let options = {
    frequency: 1000,
    enableHighAccuracy: true
  };

  this.watch = this.Geolocation.watchPosition(options).filter((p) => p.coords !== undefined)
  .subscribe((position: Geoposition) => {

    //console.log(position);

    this.zone.run(() => {
      this.lat = position.coords.latitude;
      this.lng = position.coords.longitude;
      this.speed = position.coords.speed;
      console.log(position.coords.speed);
      console.log("coba2");
    });

  });

}
// stopTracking() {
// console.log('stopTracking');
// this.backgroundGeolocation.finish();
// this.watch.unsubscribe();

// }

```

}

---

<div class="post-metadata">

**Author:** ![kvnjo](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@kvnjo](https://forum.ionicframework.com/u/kvnjo)\
**Post date:** [April 18, 2018, 3:53am UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/11 "2018-04-18T03:53:24Z")

</div>

Hi RSA…

One question again. If i’m not moving what should the speed return ??  
Is it 0 or doesnt show anything ??  
Thanks…

---

<div class="post-metadata">

**Author:** ![rsa](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@rsa](https://forum.ionicframework.com/u/rsa)\
**Post date:** [April 18, 2018, 5:16am UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/12 "2018-04-18T05:16:46Z")

</div>

If you set `public speed = 0;` then should be zero at first.  
For me speed is blank in beginning, after starting to moving it shows the moment speed (km/hrs) like cars.

---

<div class="post-metadata">

**Author:** ![kvnjo](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@kvnjo](https://forum.ionicframework.com/u/kvnjo)\
**Post date:** [April 25, 2018, 2:41am UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/13 "2018-04-25T02:41:28Z")

</div>

Hi RSA…  
Thank you for your help.Your code is already running on my device, but the app can’t run on all phones. I have tried the app in samsung galaxy a3 and the app doesnt running but the app running in asus zenfone. Does ionic not support all mobile phones ??

---

<div class="post-metadata">

**Author:** ![rsa](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@rsa](https://forum.ionicframework.com/u/rsa)\
**Post date:** [April 25, 2018, 3:57am UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/14 "2018-04-25T03:57:39Z")

</div>

What was your problem? is that crash on starting? if it crash with **can not fine index.html**  
you had to add some delay on loading in config.xml

`<preference name="SplashScreenDelay" value="3000" />`  
to  
`<preference name="SplashScreenDelay" value="7000" />`

also ionic use minimu sdk 19 you may use 16 like in same file.

```auto
<preference name="android-minSdkVersion" value="16" />
<preference name="android-targetSdkVersion" value="16" />

```

---

<div class="post-metadata">

**Author:** ![kvnjo](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@kvnjo](https://forum.ionicframework.com/u/kvnjo)\
**Post date:** [April 25, 2018, 4:22am UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/15 "2018-04-25T04:22:52Z")

</div>

the problem is when i tried the app on my samsung speed feature does not want to appear while on my asus feature speed would appear…

---

<div class="post-metadata">

**Author:** ![ioclaudio](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ioclaudio/32/126373_2.png) [@ioclaudio](https://forum.ionicframework.com/u/ioclaudio)\
**Post date:** [May 22, 2019, 10:01am UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/16 "2019-05-22T10:01:39Z")

</div>

> [@rsa](#):
>
> this.speed = Number.isNaN(location.speed \* 3.6) ? 0 : (location.speed \* 3.6);

Hi did you solve your problem?  
Are you able to show the device speed using Background geolocation?

Thank you

---

<div class="post-metadata">

**Author:** ![abhishekp](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@abhishekp](https://forum.ionicframework.com/u/abhishekp)\
**Post date:** [January 13, 2021, 2:34pm UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/17 "2021-01-13T14:34:37Z")

</div>

Hi  
I am getting the lat lng by geolocation.  
But when I am trying to build the app I am getting error. Plz tell how to resolve

 ![walktrack](https://global.discourse-cdn.com/ionicframework/original/3X/d/1/d1b8c5beb74e13930df974f37040961950c61984.png)

---

<div class="post-metadata">

**Author:** ![ghostriderus](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ghostriderus/32/135793_2.png) [@ghostriderus](https://forum.ionicframework.com/u/ghostriderus)\
**Post date:** [January 13, 2021, 3:46pm UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/18 "2021-01-13T15:46:50Z")

</div>

I see that folder not found. First try to delete and add again android project to cordova.

---

<div class="post-metadata">

**Author:** ![abhishekp](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@abhishekp](https://forum.ionicframework.com/u/abhishekp)\
**Post date:** [January 14, 2021, 4:49am UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/19 "2021-01-14T04:49:12Z")

</div>

I did that many time. I also did uninstall/install npm, but still facing the same issue

---

<div class="post-metadata">

**Author:** ![ghostriderus](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ghostriderus/32/135793_2.png) [@ghostriderus](https://forum.ionicframework.com/u/ghostriderus)\
**Post date:** [January 14, 2021, 4:57am UTC](https://forum.ionicframework.com/t/how-much-is-my-speed/99185/20 "2021-01-14T04:57:26Z")

</div>

> <https://github.com/apache/cordova-android/issues/678#issuecomment-544961990>
>
> I updated cordova-android of 7.1.4 to 8.0.0 in ionic project.
> in browser work c…orrectly with \`ionic serve\`
> but when build with \`ionic cordova android build\` throws the following error.
> 
> authenticator.xml:2: AAPT: error: resource mipmap/icon not found
> 
> how to fix this problem ?

> Create a directory : platforms\android\app\src\main\res\mipmap.  
> Then copy resources\splash.png to icon.png in the new mipmap directory.

[Next page](https://forum.ionicframework.com/t/how-much-is-my-speed/99185.md?page=2)
