I'm trying to work with the new Google Places API for Android Autocomplete feature and I wrote a test app just to see if I could get some results back and parse them correctly.
In the below sample, I just hardcoded LatLngs and a query which basically amount to "bar" in the Manhattan, NYC area. However, it consistently takes a really long time to finish (upwards of 30s even though I'm on a 40Mbps wifi connection) and doesn't return any results as far as I can tell from my Logging and from running this with the Debugger.
I'm not getting any errors and as far as I can tell I've configured the Places API correctly for use with my test app.
Anyone have any idea what could be wrong about what I'm doing here?
Test app:
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.location.places.AutocompletePrediction;
import com.google.android.gms.location.places.AutocompletePredictionBuffer;
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 java.util.concurrent.TimeUnit;
public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener{
private final String TAG = "tag";
private GoogleApiClient googleApiClient;
String query;
LatLng nwLatLng;
LatLng seLatLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
query = "bar";
nwLatLng = new LatLng(40.8130838,-73.9500107);
seLatLng = new LatLng(40.7400604,-73.9403976);
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Places.GEO_DATA_API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "connected");
LatLngBounds latLngBounds = getScreenLatLngBounds(nwLatLng, seLatLng);
Log.i(TAG, "Center of LatLngBounds=" + latLngBounds.getCenter().latitude + ", " + latLngBounds.getCenter().longitude);
PendingResult<AutocompletePredictionBuffer> pendingResult = getResults(query, latLngBounds);
runRequest(pendingResult);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
protected void onStart() {
super.onStart();
googleApiClient.connect();
}
@Override
protected void onStop() {
googleApiClient.disconnect();
super.onStop();
}
private PendingResult<AutocompletePredictionBuffer> getResults(String query, LatLngBounds latLngBounds) {
return Places.GeoDataApi.getAutocompletePredictions(googleApiClient, query, latLngBounds, null);
}
private LatLngBounds getScreenLatLngBounds(LatLng northwest, LatLng southeast) {
return new LatLngBounds.Builder().include(northwest).include(southeast).build();
}
private void awaitPendingResult(PendingResult pendingResult) {
Log.i(TAG, "Getting results for \"" + query + "\"");
AutocompletePredictionBuffer autocompletePredictions = (AutocompletePredictionBuffer)
pendingResult.await(60, TimeUnit.SECONDS);
for (AutocompletePrediction prediction : autocompletePredictions) {
String place = "PlaceID=" + prediction.getPlaceId() + "\n";
Log.i(TAG, place);
}
autocompletePredictions.release();
Log.i(TAG, "Finished getting results");
}
private void runRequest(final PendingResult pendingResult) {
new Thread(new Runnable() {
@Override
public void run() {
awaitPendingResult(pendingResult);
}
}).start();
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.googleplacestest">
<uses-permission android:name="ANDROID.PERMISSION.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="{KEY}"/>
</manifest>