Android Google Map Add AutoComplete Place search

  • by
Android Google Map Add AutoComplete Place search 01

Android Google Map Add AutoComplete Place search:

In this post we will add Google Map’s autocomplete place search textbox to our android application. In my previous post I posted on Adding Google map’s Markers to Android application.

Google Map’s autocomplete place search can be a helpful tool in Android application to allow users to search particular place on maps with few initials using Google map’s place search API. In this application, we will be using Autocomplete placeholder to search places on Google Android map, upon successfully selecting a place from the placeholder we will mark that particular google place on map with a marker and set its tittle too. So let’s begin.

Download Source code.

In your Android Application, create a new layout file with name activity_main.xml and edit it as below:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="3"
android:gravity="center_vertical|center_horizontal"
android:text="Place name :" />

<fragment
android:id="@+id/place1"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="7" />
</LinearLayout>

<TextView
android:id="@+id/txtInfo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#000"
android:padding="5dp"
android:text=""
android:textColor="#fff" />

<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
tools:context=".MapsActivity" />
</LinearLayout>

Android Google Map Add AutoComplete Place search 01

Now create a new class file with name MainActivity.java in your Android application’s default package name and edit it as below

MainActivity.java:

package com.map.parallelcodes;

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;
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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

private GoogleMap mMap;
PlaceAutocompleteFragment place1;
TextView txtInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtInfo = (TextView) findViewById(R.id.txtInfo);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
place1 = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place1);

place1.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
AddPlace(place, 1);
}

@Override
public void onError(Status status) {
}
});

}

public void AddPlace(Place place, int pno) {
try {
if (mMap == null) {
Toast.makeText(MainActivity.this, "Please check your API key for Google Places SDK and your internet conneciton", Toast.LENGTH_LONG).show();
} else {

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 4));

mMap.addMarker(new MarkerOptions().position(place.getLatLng())
.title("Name:" + place.getName() + ". Address:" + place.getAddress()));

txtInfo.setText("Name:" + place.getName() + ". Address:" + place.getAddress());

}
} catch (Exception ex) {

if (ex != null) {
Toast.makeText(MainActivity.this, "Error:" + ex.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

if(mMap!=null)
{
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
txtInfo.setText(marker.getTitle().toString() + " Lat:" + marker.getPosition().latitude + " Long:" + marker.getPosition().longitude);
return false;
}
});
}
}
}

This will set your application working. You will be able to display google map and use google autocomplete place holder to search a particular place and mark it on map.

Download Source code.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.