How to Search Location in Google Map in Android?

Hi, guys here is another useful tutorial. You might require searching locations on google map. We already learned about integrating google maps in our application in some previous posts. In this post, we will learn …

Hi, guys here is another useful tutorial. You might require searching locations on google map. We already learned about integrating google maps in our application in some previous posts. In this post, we will learn “How to Search Location in Google Map in Android?

Before moving ahead, let me tell you what exactly we are going to do. So we will display map, the user can search a location on the map, and then we will show the marker at the searched location on the map.

Creating a new Android Project

  • As always we will create a new project. So open Android Studio and create a project with Google Maps Activity.

  • When you create a project or activity with Google Maps Activity Template, a file named google_maps_api.xml is created. Inside this file you need to define the API Key to work with maps.

Getting an API Key

  • To get the API Key go to this link. And then click on GET A KEY.

  • You need to paste the key inside string tag that is created inside the file google_maps_api.xml. You can find this file inside app->res->values
  • Now you can run your application and you will see the Google Map in your Activity.

Using the PlaceAutoCompleteFragment for Searching Location

Adding PlaceAutoCompleteFragment Dependency

  • Go to your app level build.gradle file and make sure the following dependencies are added.

Adding PlaceAutoCompleteFragment in Activity

  • Now come inside the file activity_maps.xml which is created while creating the project. You need to modify your code as shown below.

Enabling Places API

  • You also need to enable Google Places API to make it work. So go to this link.
  • You need to do the same thing, click on GET A KEY on the top, and then select the same project that you selected while creating the API Key for Google Map. It will enable Google Places API and will give you another KEY. You can use this key as well, but it is not necessary as we already have the key.

Searching the Location

  • Now go to MapsActivity.java and here you will find the below code that is available by default.

  • You need to add the following code at the end of onCreate() method.

  • Now thats it, run your application.

  • As you can see it is working absolutely fine.

Search Location in Google Map Source Code

If you are having any kind of problem then you can try my source code from this GitHub repository.

[sociallocker id=1372]  How to Search Location in Google Map in Android Source Code [/sociallocker]

So thats all for this tutorial friends. I hope you found this helpful. For any query, you can leave your comments. 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