Google map integration – Android

google-map-android
 

Google map integration in Android.

One of the most important topics in Android is Google map integration. In this article, we are going to see how to integrate Google map in Android. We will create a basic map project and also set our own marker. So let’s get started.

google-map-android

Go to Android Studio and create a new project. File – New – New Project.
In the “Add an Activity to Mobile” window, Select Google Maps Activity.

google-maps-activity

Now go to google_maps_api.xml and you will see a line something like this:

Here we have to paste our API key into the YOUR_KEY_HERE section. Yes, we need an API key to work with Google map. Let’s see how to generate the API key.
Go to Google Maps APIs and click Google Maps Android API.

google-maps-android-apiClick GET A KEY located at the top right corner and a pop-up window will open.

get-a-key

Select Create a new project and name it whatever you like, you can name it same as your android studio project name.

create-new-project

Name your project and click CREATE AND ENABLE API. You will get your API key which starts with AIza. Copy that key and paste the key into the YOUR_KEY_HERE section in google_maps_api.xml.

Now run your app. By default, the marker is pointing at Sydney. We will change the marker location, the title of the marker, marker icon, and zoom level. Our MapsActivity looks something like this. Let’s change the title of the marker and the location.

I am going to change the marker location to Park Street, Kolkata, India. To change the location we need the Latitude and Longitude of that location. To find the Latitude and Longitude, just go to Google map and search your location. Google will set the marker at that location.

map-whats-here
To get lat and long just right click on the location and select “What’s here” and you will get the Latitude and Longitude of that location.

lat-long-map
Copy the latitude and longitude and replace with Sydney’s latitude and longitude. I am also going to chnage the object name from Syndey to latLng.

This is how our new MapsActivity.java.

Okay now that both the marker and title of the marker are changed, let’s change the icon of the marker.

 To change the icon of the marker, call fromResource() method of BitmapDescriptorFactory class. The fromResource() method accepts an int parameter. So we have to pass the resource id of the icon to this method. Store the icon in your drawable folder.

You can get lots of free icons for your app from here.

In your onMapReady method set your marker icon. See the code below.

Now the last thing is left is to change the zoom level.

To set the zoom level call newLatLngZoom() of CameraUpdateFactory class.

Change the float value to adjust the zoom level.

This is our final code –

That’s all. You can also change the theme of your Google map in Android. If you have any doubt, please comment below in the comment box. And if this tutorial was helpful to you, please don’t forget to share it.

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *