|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package itangqi.me.mygoogleplaces; |
| 18 | + |
| 19 | +import android.content.res.Resources; |
| 20 | +import android.net.Uri; |
| 21 | +import android.os.Bundle; |
| 22 | +import android.support.v7.app.AppCompatActivity; |
| 23 | +import android.text.Html; |
| 24 | +import android.text.Spanned; |
| 25 | +import android.util.Log; |
| 26 | +import android.view.View; |
| 27 | +import android.widget.AdapterView; |
| 28 | +import android.widget.AutoCompleteTextView; |
| 29 | +import android.widget.Button; |
| 30 | +import android.widget.TextView; |
| 31 | +import android.widget.Toast; |
| 32 | + |
| 33 | +import com.google.android.gms.common.ConnectionResult; |
| 34 | +import com.google.android.gms.common.api.GoogleApiClient; |
| 35 | +import com.google.android.gms.common.api.PendingResult; |
| 36 | +import com.google.android.gms.common.api.ResultCallback; |
| 37 | +import com.google.android.gms.location.places.Place; |
| 38 | +import com.google.android.gms.location.places.PlaceBuffer; |
| 39 | +import com.google.android.gms.location.places.PlaceLikelihoodBuffer; |
| 40 | +import com.google.android.gms.location.places.Places; |
| 41 | +import com.google.android.gms.maps.model.LatLng; |
| 42 | +import com.google.android.gms.maps.model.LatLngBounds; |
| 43 | + |
| 44 | +public class MainActivity extends AppCompatActivity |
| 45 | + implements GoogleApiClient.OnConnectionFailedListener { |
| 46 | + /** |
| 47 | + * GoogleApiClient wraps our service connection to Google Play Services and provides access |
| 48 | + * to the user's sign in state as well as the Google's APIs. |
| 49 | + */ |
| 50 | + private static final int GOOGLE_API_CLIENT_ID = 0; |
| 51 | + |
| 52 | + protected GoogleApiClient mGoogleApiClient; |
| 53 | + |
| 54 | + private PlaceAutocompleteAdapter mAdapter; |
| 55 | + |
| 56 | + private AutoCompleteTextView mAutocompleteView; |
| 57 | + |
| 58 | + private TextView mPlaceDetailsText; |
| 59 | + |
| 60 | + private TextView mPlaceDetailsAttribution; |
| 61 | + |
| 62 | + private Button mCurrentLocation; |
| 63 | + |
| 64 | + private static final String TAG = "MainActivity"; |
| 65 | + |
| 66 | + private static final LatLngBounds BOUNDS_GREATER_SYDNEY = new LatLngBounds( |
| 67 | + new LatLng(-34.041458, 150.790100), new LatLng(-33.682247, 151.383362)); |
| 68 | + |
| 69 | + @Override |
| 70 | + protected void onCreate(Bundle savedInstanceState) { |
| 71 | + super.onCreate(savedInstanceState); |
| 72 | + |
| 73 | + // Construct a GoogleApiClient for the {@link Places#GEO_DATA_API} using AutoManage |
| 74 | + // functionality, which automatically sets up the API client to handle Activity lifecycle |
| 75 | + // events. If your activity does not extend FragmentActivity, make sure to call connect() |
| 76 | + // and disconnect() explicitly. |
| 77 | + mGoogleApiClient = new GoogleApiClient.Builder(this) |
| 78 | + .enableAutoManage(this, GOOGLE_API_CLIENT_ID /* clientId */, this) |
| 79 | + .addApi(Places.GEO_DATA_API) |
| 80 | + .addApi(Places.PLACE_DETECTION_API) |
| 81 | + .build(); |
| 82 | + |
| 83 | + setContentView(R.layout.activity_main); |
| 84 | + |
| 85 | + |
| 86 | + // Retrieve the AutoCompleteTextView that will display Place suggestions. |
| 87 | + mAutocompleteView = (AutoCompleteTextView) |
| 88 | + findViewById(R.id.autocomplete_places); |
| 89 | + |
| 90 | + // Register a listener that receives callbacks when a suggestion has been selected |
| 91 | + mAutocompleteView.setOnItemClickListener(mAutocompleteClickListener); |
| 92 | + |
| 93 | + // Retrieve the TextViews that will display details and attributions of the selected place. |
| 94 | + mPlaceDetailsText = (TextView) findViewById(R.id.place_details); |
| 95 | + mPlaceDetailsAttribution = (TextView) findViewById(R.id.place_attribution); |
| 96 | + |
| 97 | + // CurrentLocation |
| 98 | + mCurrentLocation = (Button) findViewById(R.id.ll_current_location); |
| 99 | + mCurrentLocation.setOnClickListener(mOnClickListener); |
| 100 | + |
| 101 | + // Set up the adapter that will retrieve suggestions from the Places Geo Data API that cover |
| 102 | + // the entire world. |
| 103 | + mAdapter = new PlaceAutocompleteAdapter(this, android.R.layout.simple_list_item_1, |
| 104 | + mGoogleApiClient, BOUNDS_GREATER_SYDNEY, null); |
| 105 | + mAutocompleteView.setAdapter(mAdapter); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + private View.OnClickListener mOnClickListener = new View.OnClickListener() { |
| 110 | + @Override |
| 111 | + public void onClick(View view) { |
| 112 | + PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi |
| 113 | + .getCurrentPlace(mGoogleApiClient, null); |
| 114 | + result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() { |
| 115 | + @Override |
| 116 | + public void onResult(PlaceLikelihoodBuffer likelyPlaces) { |
| 117 | + if (!likelyPlaces.getStatus().isSuccess()) { |
| 118 | + // Request did not complete successfully |
| 119 | + Log.e(TAG, "Place query did not complete. Error: " + likelyPlaces.getStatus().toString()); |
| 120 | + likelyPlaces.release(); |
| 121 | + return; |
| 122 | + } |
| 123 | + String placeName = String.format("%s", likelyPlaces.get(0).getPlace().getName()); |
| 124 | + mPlaceDetailsText.setText(placeName); |
| 125 | + likelyPlaces.release(); |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + |
| 132 | + /** |
| 133 | + * Listener that handles selections from suggestions from the AutoCompleteTextView that |
| 134 | + * displays Place suggestions. |
| 135 | + * Gets the place id of the selected item and issues a request to the Places Geo Data API |
| 136 | + * to retrieve more details about the place. |
| 137 | + * |
| 138 | + * @see com.google.android.gms.location.places.GeoDataApi#getPlaceById(com.google.android.gms.common.api.GoogleApiClient, |
| 139 | + * String...) |
| 140 | + */ |
| 141 | + private AdapterView.OnItemClickListener mAutocompleteClickListener |
| 142 | + = new AdapterView.OnItemClickListener() { |
| 143 | + @Override |
| 144 | + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 145 | + /* |
| 146 | + Retrieve the place ID of the selected item from the Adapter. |
| 147 | + The adapter stores each Place suggestion in a PlaceAutocomplete object from which we |
| 148 | + read the place ID. |
| 149 | + */ |
| 150 | + final PlaceAutocompleteAdapter.PlaceAutocomplete item = mAdapter.getItem(position); |
| 151 | + final String placeId = String.valueOf(item.placeId); |
| 152 | + Log.i(TAG, "Autocomplete item selected: " + item.description); |
| 153 | + |
| 154 | + /* |
| 155 | + Issue a request to the Places Geo Data API to retrieve a Place object with additional |
| 156 | + details about the place. |
| 157 | + */ |
| 158 | + PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi |
| 159 | + .getPlaceById(mGoogleApiClient, placeId); |
| 160 | + placeResult.setResultCallback(mUpdatePlaceDetailsCallback); |
| 161 | + Log.i(TAG, "Called getPlaceById to get Place details for " + item.placeId); |
| 162 | + } |
| 163 | + }; |
| 164 | + |
| 165 | + |
| 166 | + /** |
| 167 | + * Callback for results from a Places Geo Data API query that shows the first place result in |
| 168 | + * the details view on screen. |
| 169 | + */ |
| 170 | + private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback |
| 171 | + = new ResultCallback<PlaceBuffer>() { |
| 172 | + @Override |
| 173 | + public void onResult(PlaceBuffer places) { |
| 174 | + if (!places.getStatus().isSuccess()) { |
| 175 | + // Request did not complete successfully |
| 176 | + Log.e(TAG, "Place query did not complete. Error: " + places.getStatus().toString()); |
| 177 | + places.release(); |
| 178 | + return; |
| 179 | + } |
| 180 | + // Get the Place object from the buffer. |
| 181 | + final Place place = places.get(0); |
| 182 | + |
| 183 | + // Format details of the place for display and show it in a TextView. |
| 184 | + mPlaceDetailsText.setText(formatPlaceDetails(getResources(), place.getName(), |
| 185 | + place.getId(), place.getAddress(), place.getPhoneNumber(), |
| 186 | + place.getWebsiteUri())); |
| 187 | + |
| 188 | + // Display the third party attributions if set. |
| 189 | + final CharSequence thirdPartyAttribution = places.getAttributions(); |
| 190 | + if (thirdPartyAttribution == null) { |
| 191 | + mPlaceDetailsAttribution.setVisibility(View.GONE); |
| 192 | + } else { |
| 193 | + mPlaceDetailsAttribution.setVisibility(View.VISIBLE); |
| 194 | + mPlaceDetailsAttribution.setText(Html.fromHtml(thirdPartyAttribution.toString())); |
| 195 | + } |
| 196 | + |
| 197 | + Log.i(TAG, "Place details received: " + place.getName()); |
| 198 | + |
| 199 | + places.release(); |
| 200 | + } |
| 201 | + }; |
| 202 | + |
| 203 | + private static Spanned formatPlaceDetails(Resources res, CharSequence name, String id, |
| 204 | + CharSequence address, CharSequence phoneNumber, Uri websiteUri) { |
| 205 | + Log.e(TAG, res.getString(R.string.place_details, name, id, address, phoneNumber, |
| 206 | + websiteUri)); |
| 207 | + return Html.fromHtml(res.getString(R.string.place_details, name, id, address, phoneNumber, |
| 208 | + websiteUri)); |
| 209 | + |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Called when the Activity could not connect to Google Play services and the auto manager |
| 214 | + * could resolve the error automatically. |
| 215 | + * In this case the API is not available and notify the user. |
| 216 | + * |
| 217 | + * @param connectionResult can be inspected to determine the cause of the failure |
| 218 | + */ |
| 219 | + @Override |
| 220 | + public void onConnectionFailed(ConnectionResult connectionResult) { |
| 221 | + |
| 222 | + Log.e(TAG, "onConnectionFailed: ConnectionResult.getErrorCode() = " |
| 223 | + + connectionResult.getErrorCode()); |
| 224 | + |
| 225 | + // TODO(Developer): Check error code and notify the user of error state and resolution. |
| 226 | + Toast.makeText(this, |
| 227 | + "Could not connect to Google API Client: Error " + connectionResult.getErrorCode(), |
| 228 | + Toast.LENGTH_SHORT).show(); |
| 229 | + MainActivity.this.finish(); |
| 230 | + } |
| 231 | + |
| 232 | +} |
0 commit comments