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

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.

dependency

1

compile'com.google.android.gms:play-services:8.4.0'

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.

LocationService.java

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

packagechutka.bitman.com.speedometersimplified;

importandroid.app.Service;

importandroid.content.Intent;

importandroid.location.Location;

importandroid.os.Binder;

importandroid.os.Bundle;

importandroid.os.IBinder;

importandroid.os.SystemClock;

importandroid.support.annotation.Nullable;

importcom.google.android.gms.common.ConnectionResult;

importcom.google.android.gms.common.api.GoogleApiClient;

importcom.google.android.gms.location.LocationListener;

importcom.google.android.gms.location.LocationRequest;

importcom.google.android.gms.location.LocationServices;

importjava.text.DecimalFormat;

importjava.util.concurrent.TimeUnit;

/**

* Created by vipul on 12/13/2015.

*/

publicclassLocationService extendsService implements

LocationListener,

GoogleApiClient.ConnectionCallbacks,

GoogleApiClient.OnConnectionFailedListener{

privatestaticfinallongINTERVAL=1000*2;

privatestaticfinallongFASTEST_INTERVAL=1000*1;

LocationRequest mLocationRequest;

GoogleApiClient mGoogleApiClient;

Location mCurrentLocation,lStart,lEnd;

staticdoubledistance=0;

doublespeed;

privatefinalIBinder mBinder=newLocalBinder();

@Nullable

@Override

publicIBinder onBind(Intent intent){

createLocationRequest();

mGoogleApiClient=newGoogleApiClient.Builder(this)

.addApi(LocationServices.API)

.addConnectionCallbacks(this)

.addOnConnectionFailedListener(this)

.build();

mGoogleApiClient.connect();

returnmBinder;

}

protectedvoidcreateLocationRequest(){

mLocationRequest=newLocationRequest();

mLocationRequest.setInterval(INTERVAL);

mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

}

@Override

publicintonStartCommand(Intent intent,intflags,intstartId){

returnsuper.onStartCommand(intent,flags,startId);

}

@Override

publicvoidonConnected(Bundle bundle){

try{

LocationServices.FusedLocationApi.requestLocationUpdates(

mGoogleApiClient,mLocationRequest,this);

}catch(SecurityExceptione){

}

}

protectedvoidstopLocationUpdates(){

LocationServices.FusedLocationApi.removeLocationUpdates(

mGoogleApiClient,this);

distance=0;

}

@Override

publicvoidonConnectionSuspended(inti){

}

@Override

publicvoidonLocationChanged(Location location){

MainActivity.locate.dismiss();

mCurrentLocation=location;

if(lStart==null){

lStart=mCurrentLocation;

lEnd=mCurrentLocation;

}else

lEnd=mCurrentLocation;

//Calling the method below updates the live values of distance and speed to the TextViews.

updateUI();

//calculating the speed with getSpeed method it returns speed in m/s so we are converting it into kmph

speed=location.getSpeed()*18/5;

}

@Override

publicvoidonConnectionFailed(ConnectionResult connectionResult){

}

publicclassLocalBinderextendsBinder{

publicLocationService getService(){

returnLocationService.this;

}

}

//The live feed of Distance and Speed are being set in the method below .

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.

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.

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”);

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.

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.

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)

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here:
Cookie Policy

About Simplified Coding

Simplified Coding is a blog for all the students learning programming. We are providing various tutorials related to programming and application development. You can get various nice and simplified tutorials related to programming, app development, graphics designing and animation. We are trying to make these things simplified and entertaining. We are writing text tutorial and creating video and visual tutorials as well. You can check about the admin of the blog here and check out our sitemap