Google Places API。 PlaceLikelihoodBuffer响应缓慢。 Android的

时间:2022-10-14 15:25:20

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

我正在使用PlaceLikelihoodBuffer buffer = result.await(); (在AsyncTask中),以获得我目前最有可能找到的位置。

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.)

第一次通常需要3秒才能得到回复。所以我的问题是这是否正常?还是假设要快得多?在那种情况下,可能是我缓慢的可能原因。 (ps.GoogleApiClient连接速度非常快。当APIclient连接时,PlaceDetection会运行。)

Code:

Here is Api Client:

这是Api客户端:

   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:

以下是关于获得Placelikelyhood的电话:

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

1 个解决方案

#1


0  

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.

您正在使用mGoogleApiClient.blockingConnect(),它必须在后台线程中运行,并且您正在使用PendingResult.await(),它也必须在后台线程中运行。

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():

我使用connect()而不是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():

然后在onConnected()中我使用标准回调而不是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:

首次测试,连接到我的2.5 GHz WiFi连接。在日志中,您可以看到它在26:11.239发出请求,结果发布在26:12.920,这是1.681秒的差异:

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:

接下来的测试,我连接到我的5 GHz WiFi连接,并在1.521秒内返回:

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.

下一次测试,与WiFi断开连接,连接到LTE。

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

这并不奇怪地花了一点时间:2.642秒。

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):

然而,令人惊讶的是,与连接到WiFi相比,它返回​​的结果要多得多(以前的结果每次返回4个结果,之前的日志中没有显示,这里我列出了完整列表):

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

#1


0  

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.

您正在使用mGoogleApiClient.blockingConnect(),它必须在后台线程中运行,并且您正在使用PendingResult.await(),它也必须在后台线程中运行。

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():

我使用connect()而不是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():

然后在onConnected()中我使用标准回调而不是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:

首次测试,连接到我的2.5 GHz WiFi连接。在日志中,您可以看到它在26:11.239发出请求,结果发布在26:12.920,这是1.681秒的差异:

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:

接下来的测试,我连接到我的5 GHz WiFi连接,并在1.521秒内返回:

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.

下一次测试,与WiFi断开连接,连接到LTE。

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

这并不奇怪地花了一点时间:2.642秒。

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):

然而,令人惊讶的是,与连接到WiFi相比,它返回​​的结果要多得多(以前的结果每次返回4个结果,之前的日志中没有显示,这里我列出了完整列表):

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