Android Google Maps Tutorial – Google Maps Android API

In this Android Google Maps API Tutorial, we will create a simple map to save our favourite places in SQLite Database. So basically in this tutorial we will be creating an Android Location Manager to store our favorite …

android google maps tutorial

In this Android Google Maps API Tutorial, we will create a simple map to save our favourite places in SQLite Database. So basically in this tutorial we will be creating an Android Location Manager to store our favorite places on map.

So lets begin our Android Google Maps Tutorial.

To use Google Maps we need Google Maps Android API. Now don’t worry about getting an API is very easy. First lets create our android project for this Android Google Maps Tutorial.

Android Google Maps Tutorial

  • Open android studio and create a new Android Project.
  • I named it GoogleMaps. Remember this time we will select Google Maps Activity from the predefined templates. (This will simplify the process of adding google map)
google maps activity
Google Maps Activity
  • Now click next -> and finish.
  • You will see the below screen when your android google maps tutorial project is fully loaded.
google maps api
google_maps_api.xml
  • See the highlighted text it is a link. Copy this and paste it to your browser (Make sure you are logged in your google account). You will see this.
developers console
Developers Console
  • Click on continue.
google developers console
Android Google Maps Tutorial
  • Now click on Go to credentials.
credentials
Android Google Maps Tutorial
  • Now click on Create and you will get Your API Key.
api key
Android Google Maps Tutorial
  • Now copy the API Key to your String inside google_maps_api.xml file. (From where you copied the link to create the api).
  • Now run your application and you will that the app Start Google Maps (See the screenshot of my output).
google maps
Android Google Maps Tutorial
  • For this android google maps tutorial app we will create some buttons at the bottom of the map. For buttons I have used images, you can get the images from below. Paste the images inside your drawable folder.

Download Images for Buttons

  • Now come inside activity_maps.xml and write the following xml code.

  • This code would produce the following layout. This is the main layout of this android google maps tutorial. You can change the look if you want.
activity_maps.xml
activity_maps.xml
  • We have created three buttons first one would be used to move to the current location, second will save a given location and the last one will be used for viewing the saved locations.
  • Come inside MapsActivity.java. 
  • First we will implement the following interfaces.

  • Now you will be seeing red lines, because we didn’t yet implemented the methods of the interfaces. So just put your cursor over red line and press alt+enter and click on implement methods (as shown in image).
mainactivity.java
Android Google Maps Tutorial
  • Now click ok and all the methods will be implemented.
select methods to implement
Android Google Maps Tutorial
  • Now inside the class declare the following variables.

  • Now write the following inside onCreate().

  • We need to override onStart() and onStop() to connect and disconnect to our Google Api Client. Write the following code to do this.

  • We will initialize our Map inside the overriden method onMapReady().

  • Now we will create two more methods one to get the current location getCurrentLocation() and other to move the map camera moveMap()

  • Come inside onConnected() method. This is also an overriden method, here we will call the getCurrentLocation() method.

  • When user will tap on the map for a long time we will add a marker at that position. We will also remove the previous marker. So come inside overriden method onMapLongClick().

  • To get the coordinates after the drag we will use the overriden method onMarkerDragEnd(). Write the following code inside onMarkerDragEnd().

  • Inside the onClick() method we will perform the button clicks. Write the following code inside onClick().

  • So far the full code we just created is

  • We will also need the following permission to be added in our manifest.

  • Now lets run this Android Google Maps Tutorial App. You will see the following output.

dragged location

  • If we will tap the first button (Only this button is working now). We will move the the current location.

current location

  • Bingo! Our Android Google Maps Tutorial  is working fine. Though we have to code a bit more to fully complete our Android Locator.
  • To drag the marker just long click over the marker and move your thumb to drag it. When you will press the first button it will again come to the current location. You can get my source code from my GitHub Repository from the link given below.

Get This Android Google Maps Tutorial Source from GitHub

[sociallocker id=1372]Android Google Maps Tutorial[/sociallocker]

[wp_ad_camp_1]

This Android Google Maps Tutorial is little long that is why I am breaking this part here in the next part we will add the functionality to remaining two button which is save and view.

So wait for the next part of Android Google Maps Tutorial or you can try completing it yourself. In the next part of our Android Google Maps Tutorial we will complete this app. 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