Download File
Download Project
Settings
Line Wrap
Themes
default
ambiance
bespin
dracula
eclipse
material
mbo
mdn-like
neat
solarized dark
ttcn
zenburn
MainActivity.java
/* * Copyright (C) 2015 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.google.playservices.placecomplete; import android.content.res.Resources; import android.net.Uri; import android.os.Bundle; import android.text.Html; import android.text.Spanned; import android.view.View; import android.widget.AdapterView; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.example.android.common.activities.SampleActivityBase; import com.example.android.common.logger.Log; import com.google.android.gms.location.places.AutocompletePrediction; import com.google.android.gms.location.places.GeoDataClient; import com.google.android.gms.location.places.Place; import com.google.android.gms.location.places.PlaceBufferResponse; import com.google.android.gms.location.places.Places; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.LatLngBounds; import com.google.android.gms.maps.model.RuntimeRemoteException; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; public class MainActivity extends SampleActivityBase { /** * GeoDataClient wraps our service connection to Google Play services and provides access * to the Google Places API for Android. */ protected GeoDataClient mGeoDataClient; private PlaceAutocompleteAdapter mAdapter; private AutoCompleteTextView mAutocompleteView; private TextView mPlaceDetailsText; private TextView mPlaceDetailsAttribution; private static final LatLngBounds BOUNDS_GREATER_SYDNEY = new LatLngBounds( new LatLng(-34.041458, 150.790100), new LatLng(-33.682247, 151.383362)); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Construct a GeoDataClient for the Google Places API for Android. mGeoDataClient = Places.getGeoDataClient(this, null); setContentView(R.layout.activity_main); // Retrieve the AutoCompleteTextView that will display Place suggestions. mAutocompleteView = (AutoCompleteTextView) findViewById(R.id.autocomplete_places); // Register a listener that receives callbacks when a suggestion has been selected mAutocompleteView.setOnItemClickListener(mAutocompleteClickListener); // Retrieve the TextViews that will display details and attributions of the selected place. mPlaceDetailsText = (TextView) findViewById(R.id.place_details); mPlaceDetailsAttribution = (TextView) findViewById(R.id.place_attribution); // Set up the adapter that will retrieve suggestions from the Places Geo Data Client. mAdapter = new PlaceAutocompleteAdapter(this, mGeoDataClient, BOUNDS_GREATER_SYDNEY, null); mAutocompleteView.setAdapter(mAdapter); // Set up the 'clear text' button that clears the text in the autocomplete view Button clearButton = (Button) findViewById(R.id.button_clear); clearButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mAutocompleteView.setText(""); } }); } /** * Listener that handles selections from suggestions from the AutoCompleteTextView that * displays Place suggestions. * Gets the place id of the selected item and issues a request to the Places Geo Data Client * to retrieve more details about the place. * * @see GeoDataClient#getPlaceById(String...) */ private AdapterView.OnItemClickListener mAutocompleteClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { /* Retrieve the place ID of the selected item from the Adapter. The adapter stores each Place suggestion in a AutocompletePrediction from which we read the place ID and title. */ final AutocompletePrediction item = mAdapter.getItem(position); final String placeId = item.getPlaceId(); final CharSequence primaryText = item.getPrimaryText(null); Log.i(TAG, "Autocomplete item selected: " + primaryText); /* Issue a request to the Places Geo Data Client to retrieve a Place object with additional details about the place. */ Task
placeResult = mGeoDataClient.getPlaceById(placeId); placeResult.addOnCompleteListener(mUpdatePlaceDetailsCallback); Toast.makeText(getApplicationContext(), "Clicked: " + primaryText, Toast.LENGTH_SHORT).show(); Log.i(TAG, "Called getPlaceById to get Place details for " + placeId); } }; /** * Callback for results from a Places Geo Data Client query that shows the first place result in * the details view on screen. */ private OnCompleteListener
mUpdatePlaceDetailsCallback = new OnCompleteListener
() { @Override public void onComplete(Task
task) { try { PlaceBufferResponse places = task.getResult(); // Get the Place object from the buffer. final Place place = places.get(0); // Format details of the place for display and show it in a TextView. mPlaceDetailsText.setText(formatPlaceDetails(getResources(), place.getName(), place.getId(), place.getAddress(), place.getPhoneNumber(), place.getWebsiteUri())); // Display the third party attributions if set. final CharSequence thirdPartyAttribution = places.getAttributions(); if (thirdPartyAttribution == null) { mPlaceDetailsAttribution.setVisibility(View.GONE); } else { mPlaceDetailsAttribution.setVisibility(View.VISIBLE); mPlaceDetailsAttribution.setText( Html.fromHtml(thirdPartyAttribution.toString())); } Log.i(TAG, "Place details received: " + place.getName()); places.release(); } catch (RuntimeRemoteException e) { // Request did not complete successfully Log.e(TAG, "Place query did not complete.", e); return; } } }; private static Spanned formatPlaceDetails(Resources res, CharSequence name, String id, CharSequence address, CharSequence phoneNumber, Uri websiteUri) { Log.e(TAG, res.getString(R.string.place_details, name, id, address, phoneNumber, websiteUri)); return Html.fromHtml(res.getString(R.string.place_details, name, id, address, phoneNumber, websiteUri)); } }