0

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>
Deividi Cavarzan
  • 10,034
  • 13
  • 66
  • 80
ethan123
  • 1,084
  • 2
  • 14
  • 26
  • 1
    No bar in Manhattan is indeed a problem... Seriously, what response do you get from Google? – shkschneider May 07 '15 at 13:38
  • Getting 0 results 05-07 16:38:19.195 8128-8128/com.googleplacestest I/tag﹕ connected 05-07 16:38:19.195 8128-8128/com.googleplacestest I/tag﹕ Center of LatLngBounds=40.776572099999996, -73.94520415 05-07 16:38:19.195 8128-8169/com.googleplacestest I/tag﹕ Getting results for "restaurant" 05-07 16:39:01.635 8128-8169/com.googleplacestest I/tag﹕ Finished getting results – ethan123 May 07 '15 at 13:39
  • What is the http response? Are you sure your `query` string is properly formatted? And, FYI - The places API is limited to [60 responses](http://stackoverflow.com/questions/9614258/how-to-get-20-result-from-google-places-api) at the moment. – wahwahwah May 07 '15 at 13:50
  • Also, [bar](https://developers.google.com/places/supported_types) is a supported Places type – wahwahwah May 07 '15 at 13:52
  • @wahwahwah I think you're thinking about the web api. I'm referring to the new Android api, which doesn't require an HTTP request. See [this](https://developers.google.com/places/android/autocomplete#get-predictions) – ethan123 May 07 '15 at 13:53

1 Answers1

2

Figured out my bug. It was dumb. Had my api key in the wrong block. The manifest should look like:

<?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"/>

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="{API KEY}"/>

        <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>
</manifest>

Otherwise, everything else was fine. It works great and I got 5 results!

ethan123
  • 1,084
  • 2
  • 14
  • 26