How to Implement Google Place API in Android Studio.
Creating New Project.
Open android studio and create a new project.
File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.
First we need to add Library to our project.
compile 'com.google.android.gms:play-services:8.4.0'
Create Xml file in project.
Open => app => res => layout - activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="24dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/icons"
android:elevation="2dp"
android:orientation="vertical"
android:padding="8dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_from"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtFrom"
android:layout_width="match_parent"
android:layout_height="40dp"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="From" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<EditText
android:id="@+id/edtTo"
android:layout_width="match_parent"
android:layout_height="40dp"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="To" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button
android:id="@+id/find"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Find Now"
android:textColor="@color/icons" />
</LinearLayout>
Create the Java file in project.
Open app => main => src = FragmentFind.java
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.conn.findroute.R;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocomplete;
public class FragmentFind extends Fragment {
Button mFind;
EditText mFrom, mTo;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bus, container, false);
init(view);
eventListener();
return view;
}
/*
* Initialize UI elements
* */
void init(View v) {
mFind = (Button) v.findViewById(R.id.find);
mFrom = (EditText) v.findViewById(R.id.edtFrom);
mTo = (EditText) v.findViewById(R.id.edtTo);
}
/*
* Click event handler
* */
private void eventListener() {
mFind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Snackbar.make(v, "Coming Soon", Snackbar.LENGTH_LONG).show();
Intent in = new Intent(getActivity(), RouteListActivity.class);
startActivity(in);
}
});
mFrom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent intent =
new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.build(getActivity());
startActivityForResult(intent, 1);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
});
mTo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent intent =
new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.build(getActivity());
startActivityForResult(intent, 2);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == getActivity().RESULT_OK) {
Place place = PlaceAutocomplete.getPlace(getActivity(), data);
Log.i("FIND", "Place: " + place.getName());
mFrom.setText(place.getName());
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(getActivity(), data);
// TODO: Handle the error.
Log.i("MAP", status.getStatusMessage());
} else if (resultCode == getActivity().RESULT_CANCELED) {
// The user canceled the operation.
}
}
if (requestCode == 2) {
if (resultCode == getActivity().RESULT_OK) {
Place place = PlaceAutocomplete.getPlace(getActivity(), data);
Log.i("FIND", "Place: " + place.getName());
mTo.setText(place.getName());
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(getActivity(), data);
// TODO: Handle the error.
Log.i("MAP", status.getStatusMessage());
} else if (resultCode == getActivity().RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
}
Add Internet permission in your manifest.
<uses-permission android:name="android.permission.INTERNET" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="key"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
Creating New Project.
Open android studio and create a new project.
File => New => New Project => Configure your new project => Select the form factor yours app will run on => Add an Activity to Mobile => Customize the Activity => Finish.
First we need to add Library to our project.
compile 'com.google.android.gms:play-services:8.4.0'
Create Xml file in project.
Open => app => res => layout - activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="24dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/icons"
android:elevation="2dp"
android:orientation="vertical"
android:padding="8dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_from"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtFrom"
android:layout_width="match_parent"
android:layout_height="40dp"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="From" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<EditText
android:id="@+id/edtTo"
android:layout_width="match_parent"
android:layout_height="40dp"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="To" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button
android:id="@+id/find"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Find Now"
android:textColor="@color/icons" />
</LinearLayout>
Create the Java file in project.
Open app => main => src = FragmentFind.java
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.conn.findroute.R;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocomplete;
public class FragmentFind extends Fragment {
Button mFind;
EditText mFrom, mTo;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bus, container, false);
init(view);
eventListener();
return view;
}
/*
* Initialize UI elements
* */
void init(View v) {
mFind = (Button) v.findViewById(R.id.find);
mFrom = (EditText) v.findViewById(R.id.edtFrom);
mTo = (EditText) v.findViewById(R.id.edtTo);
}
/*
* Click event handler
* */
private void eventListener() {
mFind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Snackbar.make(v, "Coming Soon", Snackbar.LENGTH_LONG).show();
Intent in = new Intent(getActivity(), RouteListActivity.class);
startActivity(in);
}
});
mFrom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent intent =
new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.build(getActivity());
startActivityForResult(intent, 1);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
});
mTo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent intent =
new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
.build(getActivity());
startActivityForResult(intent, 2);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == getActivity().RESULT_OK) {
Place place = PlaceAutocomplete.getPlace(getActivity(), data);
Log.i("FIND", "Place: " + place.getName());
mFrom.setText(place.getName());
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(getActivity(), data);
// TODO: Handle the error.
Log.i("MAP", status.getStatusMessage());
} else if (resultCode == getActivity().RESULT_CANCELED) {
// The user canceled the operation.
}
}
if (requestCode == 2) {
if (resultCode == getActivity().RESULT_OK) {
Place place = PlaceAutocomplete.getPlace(getActivity(), data);
Log.i("FIND", "Place: " + place.getName());
mTo.setText(place.getName());
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(getActivity(), data);
// TODO: Handle the error.
Log.i("MAP", status.getStatusMessage());
} else if (resultCode == getActivity().RESULT_CANCELED) {
// The user canceled the operation.
}
}
}
}
Add Internet permission in your manifest.
<uses-permission android:name="android.permission.INTERNET" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="key"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
No comments:
Post a Comment