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 “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.

So thats all for this tutorial friends. I hope you found this helpful. For any query, you can leave your comments. Thank You ๐Ÿ™‚

10 thoughts on “How to Search Location in Google Map in Android?”

  1. I step over all above guide but i get “error uses or overrides a deprecated api. recompile with -xlint:deprecation for details.” message during debug and the app display nothing

    Can anybody give me advice

    Reply
  2. How to handle with entered text in search if place is not found. can I able to reterive the text from search to keep input in other view like edittext from which I can call other api (may be geocode service) to get location lat long.

    Reply
  3. Hi ,

    I have one problem when i click on edittext for search it is automatically close after 1 sec. please help me how to resolve this.

    Thanks

    Reply
    • I have found the solution…just upgrade your google place services dependency

      From => implementation ‘com.google.android.gms:play-services-places:12.0.1’

      To => implementation ‘com.google.android.libraries.places:places:2.0.0’

      And change your AutocompleteFragment in xml file and in your MapsActivity.java class as below:

      Reply
  4. Implementation was done successfully as your way(Google Map Api & Google Place Api). But when click search view the autocomplete view come over and disappear(same keyboard also come and disappear). If sometime it set correctly but at that time if we search and it give response as Not found. Please let me know what will be an error. ThankYou

    Reply
  5. I appreciate your effort, your work. Very Simple and easy way. But there are few problems like you have used here PlaceAutocompleteFragment which is deprecated in androidx suport library, and other one is the fragment is not useful to me for searching purpose. When I click on search bar the fragment opens but suddenly it closes. Guide me How I have to handle this situation?
    Thank You. Best Regards.

    Reply

Leave a Comment