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.
Table of Contents
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' //these two lines should be added implementation 'com.google.android.gms:play-services-maps:12.0.1' implementation 'com.google.android.gms:play-services-places:12.0.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="net.simplifiedcoding.locationsearchexample.MapsActivity"> <fragment android:id="@+id/place_autocomplete_fragment" android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" android:layout_width="match_parent" android:layout_height="wrap_content" /> <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/place_autocomplete_fragment" /> </RelativeLayout> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package net.simplifiedcoding.locationsearchexample; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; LatLng sydney = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); } } |
- You need to add the following code at the end of onCreate() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { @Override public void onPlaceSelected(Place place) { mMap.clear(); mMap.addMarker(new MarkerOptions().position(place.getLatLng()).title(place.getName().toString())); mMap.moveCamera(CameraUpdateFactory.newLatLng(place.getLatLng())); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 12.0f)); } @Override public void onError(Status status) { } }); |
- 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 🙂

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.
It is very helpful tutorial …explained in so easy and understandable way …Thank You so much .!!!!!
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
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.
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
i have same problem. please help
Did you solve this..??
I am facing the same issue.. AutoSearch edit text disappears immediately after clicking on it
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:
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
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.