Google Maps Distance Calculator using Google Maps API

Hello guys, welcome to a new Android Tutorial. In this tutorial we will be creating a Google Maps Distance Calculator Android Application. I have already posted a Google Maps Tutorial. I would suggest you to go through …

google maps distance calculator

Hello guys, welcome to a new Android Tutorial. In this tutorial we will be creating a Google Maps Distance Calculator Android Application.

I have already posted a Google Maps Tutorial. I would suggest you to go through the previous Google Maps Tutorial before following this Google Maps Distance Calculator Tutorial.

So in this app we will calculate the distance between two coordinates of the map. We will also draw a path between the given coordinates. So lets start.

Google Maps Distance Calculator Demo

You can check out this video demo before moving ahead to the tutorial.


Now if you want to create the same then move ahead to this Google Maps Distance Calculator Tutorial.

Creating the Android Project

  • So open Android Studio and create a new project. Now you have to select Maps Activity. I will not be discussing in details. You can check the previous google maps tutorial.
  • You also need your API Key, which you can get from google developer console. I have explained these stuffs on previous post. But one more thing here we need to do. We have the API key for Android. But for Google Maps Distance Calculator App we also need an API Key for Server.
  • Go to Google Maps Direction API. And click on GET A KEY.

google maps direction api

  •  Now select your project and click on continue.

select project

  • Now go to New Credential  and create a Server Key (I am assuming you already have the Android Key).

serverk key

  • Note down your server key and come back to your project in Android Studio.

Adding Google Maps Android API utility and Volley Library

  • In this Google Maps Distance Calculator (It is clear from the name) we need to calculate the distance between two coordinates from the map. And for this I will be using this library. So first we have to add this library to our gradle.build dependencies block.
  • We also need to send an HttpRequest to get the path between two coordinates. For this I will be using Volley Library.
  • So go inside your gradle.build file and add the following dependencies.

  • Now come to activity_maps.xml and write the following code. It will create our layout.

  • As you can see we have only 3 buttons on the layout and a fragment to display maps. The buttons are to set the coordinates and calculate distance. When the user will calculate the distance we will also show the path between the coordinates.
  • Now come to MapsActivity.java and write the following code.

  • The above code is already explained in my previous google maps tutorial. Till here we are able to get the two coordinates from the map. The final things we need to do for our Google Maps Distance Calculator App is:
    • We have to calculate the distance between the coordinates.
    • We have to draw a path between the coordinates.
  • As we are using Google Maps API Utility Library, calculating distance is extremely easy. We can use the following code to calculate the distance between coordinates.

  • To draw the path between the coordinates we need to know the path. This is a little difficult as we need to call the Google Maps Direction API. For this first we will create our request URL.  For this we can use the following code. (Replace SERVER-KEY with your Server Key that you created in your Google Developer’s Account).

  • The above method would give us a URL as a string. We need to send a get request the the URL and in return (if successful) we will get JSON.
  • To send the request we will use volley.

  • These two methods will draw the route on the map. I got this from Stack Overflow.

  • The final code of our MapsActivity.java for this Google Maps Distance Calculator  would be.

  • Lastly for this Google Maps Distance Calculator you need to add the following permissions to your AndroidManifest.xml

  • Now thats all, just run your Google Maps Distance Calculator(Use a real device).
google maps distance calculator
Google Maps Distance Calculator
  • Bingo! Our Google Maps Distance Calculator is Working absolutely fine. You can get my source code from below.

[sociallocker id=1372] Google Maps Distance Calculator[/sociallocker]

[wp_ad_camp_1]

 Thats all for this Google Maps Distance Calculator.Please share this post if you found it helpful to support us. And also feel free to ask if having any query regarding this Google Maps Distance Calculator. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x