Loading...

Google Maps Android API Adding Search Bar: Part 3

In my previous Google Maps Android API tutorials you have learned to setup google map API in your android application and get the current location with google map cursor. Google map has lots of features and we can customize google map according to our own needs. In this tutorial, you will learn to add search bar in google map. When the user type and search any places, that place will come in the screen with google map cursor.

If you have not worked with google map API before, don’t worry. It is very easy to work with google map android API. Here you don’t need to know lots of java code, just some lines of java code work perfectly.

Related:
YouTube Android API Integration: Getting Started with YouTube Android API
Embed and Play MP3 Songs/Music in Android Application Using WebView

Prerequisite:

As Google Maps Android API tutorial post becomes too longer, I have divided it into different part. So if you have not followed my previous google maps android api’s tutorials please go through part 1 and part 2.

Google Maps Android API Getting Started Tutorial: Part 1
Get Current Location - Google Maps Android API Tutorial: Part 2

Android Example: How to Search in Google Map Android API V2

Android Example: How to Search in Google Map Android API V2


Let’s continue with previous project Google Maps Android API where you’ve learnt to find or get current location and add marker to our location. Here you will learn to search some places or countries in google map by typing name in search bar.

Open your activity_maps.xml file and a LinearLayout as root layout and add EditText and Button inside LinearLayout. EditText is used to type location name to search in map, Button is for search location after typing something in address bar. Give an id to edit text and button and add onClick attribute in button. Following is the complete content of maps XML layout file.

res/layout/activity_maps.xml

Now open your java activity file MapsActivity.java and add following lines of code.


public void onMapSearch(View view) {
    EditText locationSearch = (EditText) findViewById(R.id.editText);
    String location = locationSearch.getText().toString();
    List<Address>addressList = null;
if (location != null || !location.equals("")) { Geocoder geocoder = new Geocoder(this); try { addressList = geocoder.getFromLocationName(location, 1); } catch (IOException e) { e.printStackTrace(); } Address address = addressList.get(0); LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude()); mMap.addMarker(new MarkerOptions().position(latLng).title("Marker")); mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); } }


The complete java code of MapsActivity.java file will look like below.

MapsActivity.java


Now run your Google Maps Android API Adding Search Bar: Part 3 application and search any place in the search box and click on SEARCH button, the marker move to that location which will look like above screenshot and demo video.

What Next: Changing Map Types - Google Maps Android API Tutorial Part 4

Download Complete Project

You can download complete example project source code from GitHub.
Tutorial 976616792829890590
Home item