Android SpeedoMeter Tutorial using Google Location Service

Hi there, in this Android SpeedoMeter Tutorial you will learn how to fetch your live speed, distance and duration on your android app as you move around with your phone. This Android SpeedoMeter app makes use of the Google’s Location Service to calculate the speed of your mobile. It’s quite simple the way it functions. You can see the sample output of this Android Speedometer in the below image.

android speedometer tutorial
Android Speedometer

That’s what you are gonna get through in this project. So, Let’s get started with it.

Creating our Android SpeedoMeter

New Android Studio Project

So now lets begin creating our Android Speedometer.

  • As usual, begin with creating a new Android Studio Project with empty Activity.
  • To access the Google’s Location Service, add google play services dependency to your app level gradle and then sync it.

Creating a Location Service

  • Create a class named LocationService.java and add the following code. It consists of a method called updateUI() which updates the live values of the Speed, Distance and Duration.

  • Now, when we have created the Location Service, its time to configure the uses of this service and that’ll be done in MainActivity.java. Hence, add the following code to your MainActivity.java. It consists of  methods to bind and unbind  the location service  as per required.

  • By now, we are done with the Java part.

Creating User Interface

  • Now, we need to configure the activity_main.xml. Add the following code to activity_main.xml which consists of TextViews to display the speed, distance and duration and some buttons.

  • We are almost done. Just update the AndroidManifest.xml and mention the Location Service as it is in the code below.
  • Please note that the app will only work below Android Lollipop versions. If you want to make the app work in above Android Lollipop then you need to ask the permissions at run time. To modify this app and ask the permission at run time you can refer to this Android Marshmallow Permission Example Tutorial.

  • And you did it.  Run the app now and hit the start calculating button. It would ask you to enable your gps. As you do, its gonna fetch your location and then show your live speed and all.
  • If you are facing some problems then you can download my source code from the link given below.

If you still find some problem, let me know in the comments section I will try my best to sort out your queries. Thank You 🙂

25 thoughts on “Android SpeedoMeter Tutorial using Google Location Service”

  1. nice tutorial.. can u tell me how Uber or Ola calculates live distance?? and what’s difference between these all
    Location.distanceTo()
    Location.distanceBetween()
    SphericalUtil.computeDistanceBetween()

    Reply
    • It is very difficult to comment onto how ola calculates live distance as there are numerous ways. As as far these methods you mentioned are concerned, as there name suggests, these methods are used to calculate distance between two geographical locations .For example:-
      Location locationA = new Location(“point A”);

      locationA.setLatitude(latA);
      locationA.setLongitude(lngA);

      Location locationB = new Location(“point B”);
      locationB.setLatitude(latB);
      locationB.setLongitude(lngB);

      float distance = locationA.distanceTo(locationB);

      Reply
      • i know these all calculate distance between two locations. i wanted to know the difference between these three. i have made taxi sharing app for a client in which i used direction api for estimated distance and location.distanceBetween() method for live distance calculation. i wanted to know if there’s a better approach to do the same .thanks for replying.

        Reply
  2. Sir tutorials is good for me Thanks..

    But i have one query,
    When I run my app speed continuously increasing after some seconds..
    I am a beginner, maybe this question is nasty one but please reply ASAP..
    Thanks 🙂

    Reply
  3. hey, the app doesnt ask for activation of gps once i click start calculating button it just gets stuck on trying to get my location.

    Reply
  4. First, thx for your awesome work.

    I have one Problem, using your app it’s searching all time “getting location” and nothing happens. GPS is on im on outside still no works, debug mod says all is ok, any idea? thx

    Reply
  5. Hello sir this code work fine for me . i want to show path on map with this code i get current location from on_location_changed method.
    My question is this that how can i get last location when i press stop button in the above code.??
    Please help me I am just stuck in this for 2 weeks.

    Reply
  6. It works fine. But some times it start increasing distance(10 – 400 meters) without even I move from my place. what should i do for its accuracy

    Reply
  7. When I start the application, it says getting location forever. It does not asks for any permissions. Please help. I need to submit this project in my college

    Reply
  8. Hi, great code
    But in the dependencies (compile ‘com.google.android.gms:play-services:8.4.0’) this cause the app to crash at run time. what is the fix or it says (All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 28.0.0, 23.0.0. Examples include)

    Please help?

    Reply

Leave a Comment