简体   繁体   中英

Google Places API. PlaceLikelihoodBuffer slow response. Android

I'm using PlaceLikelihoodBuffer buffer = result.await(); (In an AsyncTask ), to get the most likely place i'm located at the moment.

The code works perfectly, but the response is very slow, especially the first time I open my application and run the search/method .

First time it usually takes 3 seconds to get a response. So my question is if this is normal? or is it suppose to be much quicker? And in that case what could be the possible reason for my slowness. (ps. GoogleApiClient connect very fast. PlaceDetection is runs when APIclient gets connected.)

Code:

Here is Api Client :

   mGoogleApiClient = new GoogleApiClient.Builder(activity)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)                  
            .addApi(Places.PLACE_DETECTION_API)
            .addApi(Places.GEO_DATA_API)
            .build();

    mGoogleApiClient.blockingConnect();

Here is call to get Placelikelyhood :

PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
PlaceLikelihoodBuffer buffer = result.await();

You're using mGoogleApiClient.blockingConnect() , which must run in a background thread, and you're using PendingResult.await() , which also must run in a background thread.

It seems that the extra overhead of using background threads might be the cause of the extra latency.

Note that another cause could be the speed of your data connection.

And, yet another cause for how long it takes to return results is how many items are in the results based on the current location.

I just tested it using just callbacks and no background threads, and tested the first launch behavior with different data connection types (I made sure to kill it from the task list before launching as well).

I used connect() instead of blockingConnect() :

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Places.PLACE_DETECTION_API)
            .addApi(Places.GEO_DATA_API)
            .build();

    //mGoogleApiClient.blockingConnect();
    mGoogleApiClient.connect();

Then in onConnected() I used the standard callback instead of PendingResult.await() :

@Override
public void onConnected(Bundle bundle) {

    Log.i(TAG, "mGoogleApiClient connected");

    PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
            .getCurrentPlace(mGoogleApiClient, null);
    result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
        @Override
        public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
            for (PlaceLikelihood placeLikelihood : likelyPlaces) {
                Log.i(TAG, String.format("Place '%s' has likelihood: %g",
                        placeLikelihood.getPlace().getName(),
                        placeLikelihood.getLikelihood()));
            }
            likelyPlaces.release();
        }
    });
}

First test, connected to my 2.5 GHz WiFi connection. In the logs, you can see it made the request at 26:11.239 and the results came in at 26:12.920 , which is a difference of 1.681 seconds:

06-25 00:26:11.239  20257-20257/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ mGoogleApiClient connected
06-25 00:26:12.920  20257-20257/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Tocher Services Inc' has likelihood: 0.250000
//........

Next test, I connected to my 5 GHz WiFi connection, and it came back in 1.521 seconds:

06-25 00:51:24.385  24193-24193/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ mGoogleApiClient connected
06-25 00:51:25.906  24193-24193/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Tocher Services Inc' has likelihood: 0.250000

Next test, disconnected from WiFi, connected to LTE.

This, not surprisingly took a little longer: 2.642 seconds.

However, surprisingly, it came back with a lot more results than when connected to WiFi (previous results came back with 4 results each time, not shown in the previous logs, here I put the full list):

06-25 00:57:45.767  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ mGoogleApiClient connected
06-25 00:57:48.409  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Navlet's Garden Center' has likelihood: 0.250000
06-25 00:57:48.409  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Safeway' has likelihood: 0.0900000
06-25 00:57:48.419  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Caffino' has likelihood: 0.0800000
06-25 00:57:48.419  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'CVS Pharmacy' has likelihood: 0.0700000
06-25 00:57:48.419  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Devon Apartments' has likelihood: 0.0600000
06-25 00:57:48.419  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Strawberry Fields DJ Co' has likelihood: 0.0500000
06-25 00:57:48.419  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Yanni's Greek Cafe' has likelihood: 0.0400000
06-25 00:57:48.419  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Mother India' has likelihood: 0.0300000
06-25 00:57:48.419  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Valero' has likelihood: 0.0200000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place '76' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'The UPS Store' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Chateau Pleasant Hill' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Carland' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Chateau Pleasant Hill' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'El Mariachi Mexican Grill' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Alhambra Hills Realty' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Starbucks' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Stepping Stones Learning Center' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Intercontinental Services' has likelihood: 0.0100000
06-25 00:57:48.429  26062-26062/com.currentplace.daniel.currentplace I/CurrentPlaceTest﹕ Place 'Fitness Evolution' has likelihood: 0.0100000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM