New Post has been published on Simplified Coding
New Post has been published on https://www.simplifiedcoding.net/how-to-search-location-in-google-map-in-android/
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.
1 Creating a new Android Project
1.2 Using the PlaceAutoCompleteFragment for Searching Location
1.2.1 Adding PlaceAutoCompleteFragment Dependency
1.2.2 Adding PlaceAutoCompleteFragment in Activity
1.2.3 Enabling Places API
1.3 Searching the Location
2 Search Location in Google Map Source Code
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.
To get the API Key go to this link. And then click on GET A KEY.
(adsbygoogle = window.adsbygoogle || []).push();
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.
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.
<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>
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.
Now go to MapsActivity.java and here you will find the below code that is available by default.
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.
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.
 How to Search Location in Google Map in Android Source Code
So thats all for this tutorial friends. I hope you found this helpful. For any query, you can leave your comments. Thank You đ