Google places API implementation to retrieve the location details in Android

Our world is moving at very fast phase, technologically. As newer technologies develop, the world becomes smaller and smaller and we humans, being very curious, try to explore as many things as possible.

What is Google Place API:

Google Places API for android is such a thing. With it, the world is now in your hands. If you are lost or need to locate businesses or places nearby, you don’t need to find a guide or a map to help you, Google is now providing Google Places API.

Using the Google Places API for Android, you can build location-aware apps that respond contextually to the local businesses and other places near the device. This means that you can build rich apps based on places that mean something to the user, to complement the straightforward geographic-based services offered by the Android location services.

The release of Android places API not only simplifies the data access but also spares the developers from keeping track of latitudes and longitudes. Earlier, to access place data in Android, one had to retrieve all the information from a web service by passing various parameters like latitudes and longitudes to it. The API used at that time was Google Maps API. But now thankfully, nothing of this sort needs to be done. As the new Android places API is powerful enough to detect your current location and retrieve all the place data automatically.

Places API is integrated with google maps search which offers more consistent search across Maps and the Places API.

Features of Google Places API:

1. Place Picker UI widget
2. Auto Complete UI widget
3. GeoData Api
4. PlaceDetectionApi
5. Current Place
6. Place Report and Details

Using the above , you can build rich apps based on places that mean something to user. Google has replaced the type restriction parameter with a new type search parameter.

Type search works similarly to types restriction, but it only supports one type per request. Requests using the types parameter and those specifying multiple types (for example, types = hospital | pharmacy | doctor) will not work from now. Request type with multiple type will no longer supported. To ensure the best possible results for the user, do use single type in search request.

Google have also added autocomplete functionality to the place picker UI widget that helps users communicate their current location, such as places, address or location on the map, which makes even easier to pick specific place by starting to type its name or address. If you already using Place Picker in your app, it will automatically gain the autocomplete feature with no action required on your part.

Whenever your app displays information about places sourced from the Google Places API for Android, the app must also show all relevant attributions that are returned by the API. See the documentation on displaying attributions.

Some apps using google Places API are:

WhatsApp :

WhatsApp uses Google Maps API and Google Places API, helps you keep in touch with friends through mobile messaging that allows to send your location.

Citi Bike :

Citi Bike uses Google Maps Direction API and Google Places API, to find the bike nearest you with Citi Bikes.

Dash :

Dash uses Google Maps Geocoding API, Google Maps Roads API, Google Maps Roads API, this app provides real-time diagnostics for drivers’ cars, helping them save time and money.

Expedia :

Expedia used Google Maps Android API, Google Maps Geocoding API, Google Places API, combines into one form of app which helps you to travel around the world and back, using Expedia to find hotels in the center of the action.

Sun Surveyor :

Sun Surveyor uses Google Maps Elevation API, Google Maps Time Zone API, Google Maps SDK for android, which brings augmented reality to photographers using Google Maps APIs.

Harley Davidson : 

Harley Davidson uses Google Places API, Google Maps Android API, helps motorcyclists plan and share road trips on any platform.

Implementation in Android:

On Android, you can add the autocomplete widget as a Fragment and add an event listener to retrieve the autocompleted place reference back in the application. Alternatively, you can invoke the autocomplete widget with an Intent.

Adding a Fragment In the XML layout file for your Activity:

<fragment
      android:id=”@+id/place_autocomplete_fragment”
      android:layout_width=”match_parent”
      android:layout_height=”wrap_content”
android:name=”com.google.android.gms.location.places.ui.PlaceAutocompleteFragment”
>

Adding an Event Listener in your Activity’s onCreate() method:

PlaceAutocompleteFragment fragment = (PlaceAutocompleteFragment)
     getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

fragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
      @Override
      public void onPlaceSelected(Place place) { // Handle the selected Place
             Log.i(TAG, “Place: ” + place.getName());
         String placeDetailsStr = place.getName() + “n”
                  + place.getId() + “n”
                  + place.getLatLng().toString() + “n”
                  + place.getAddress() + “n”
                  + place.getAttributions();
      txtPlaceDetails.setText(placeDetailsStr);

}
@Override
public void onError(Status status) { // Handle the error
       Log.i(TAG, “An error
}

Creating an intent to invoke the autocomplete widget:

try {
      Intent intent =
            new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN).build(this);
      startActivityForResult(intent, PLACE_AUTOCOMPLETE_REQUEST_CODE);
} catch (GooglePlayServicesRepairableException e) {
      GooglePlayServicesUtil
            .getErrorDialog(e.getConnectionStatusCode(), getActivity(), 0);
} catch (GooglePlayServicesNotAvailableException e) {
      // Handle the exception
}

public void onPickButtonClick(View v) {
      // Construct an intent for the place picker
      try {
            PlacePicker.IntentBuilder intentBuilder =
                  new PlacePicker.IntentBuilder();
Intent intent = intentBuilder.build(this);
            // Start the intent by requesting a result,
            // identified by a request code.
            startActivityForResult(intent, REQUEST_PLACE_PICKER);

      } catch (GooglePlayServicesRepairableException e) {
            // …
      } catch (GooglePlayServicesNotAvailableException e) {
            // …
      }
}

@Override
protected void onActivityResult(int requestCode,
            int resultCode, Intent data) {

       if (requestCode == REQUEST_PLACE_PICKER
            && resultCode == Activity.RESULT_OK) {

                  // The user has selected a place. Extract the name and address.
            final Place place = PlacePicker.getPlace(data, this);
 

            final CharSequence name = place.getName();
            final CharSequence address = place.getAddress();
            String attributions = PlacePicker.getAttributions(data);
            if (attributions == null) {
                  attributions = “”;
            }

                  mViewName.setText(name);
            mViewAddress.setText(address);
            mViewAttributions.setText(Html.fromHtml(attributions));
 

      } else {
            super.onActivityResult(requestCode, resultCode, data);
      }
}

A Place is defined as a physical space that has a name. Another way of thinking about place is that it’s anything find on a map which includes local businesses, point of interest and geographic locations.

In API, place is represented by Place interface, which includes information such as name of the place and its address, geographical locations, place ID, number, place, type, website URL, and so.

In this Blog, you have learned how to use the Place Picker component, guess the user’s place, present them with predictive results when searching, and find a place based on a given ID. The Places API is a powerful tool for making your apps aware of the user’s location to provide them with contextual information.

Android Team,
Mallow Technologies.

Leave a Reply

%d bloggers like this: