Google 个地点 API。 PlaceLikelihoodBuffer 响应缓慢。 Android
Google Places API. PlaceLikelihoodBuffer slow response. Android
我正在使用 PlaceLikelihoodBuffer buffer = result.await();
(在 AsyncTask
中),以获得我目前最有可能所在的位置。
代码运行完美,但响应非常慢,尤其是我第一次打开我的应用程序和运行 search/method。
第一次通常需要3秒才能得到回应。所以我的问题是这是否正常?还是应该快得多?在那种情况下,我行动迟缓的可能原因是什么。 (ps。GoogleApiClient
连接速度非常快。PlaceDetection
连接 APIclient
时 运行 秒。)
代码:
这里是Api Client
:
mGoogleApiClient = new GoogleApiClient.Builder(activity)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Places.PLACE_DETECTION_API)
.addApi(Places.GEO_DATA_API)
.build();
mGoogleApiClient.blockingConnect();
这里是获取 Placelikelyhood
:
的调用
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
PlaceLikelihoodBuffer buffer = result.await();
您正在使用 mGoogleApiClient.blockingConnect()
,它必须在后台线程中 运行,并且您正在使用 PendingResult.await()
,它也必须在后台线程中 运行 .
似乎使用后台线程的额外开销可能是额外延迟的原因。
请注意,另一个原因可能是您的数据连接速度。
而且,return 结果需要多长时间的另一个原因是基于当前位置的结果中有多少项目。
我只是使用回调而不使用后台线程对其进行了测试,并使用不同的数据连接类型测试了首次启动行为(我也确保在启动之前将其从任务列表中删除)。
我用了 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();
然后在 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();
}
});
}
第一次测试,连接到我的 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
//........
下一次测试,我连接到我的 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
下次测试,断开 WiFi,连接到 LTE。
毫不奇怪,这花了一点时间:2.642 秒。
然而,令人惊讶的是,它返回的结果比连接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
我正在使用 PlaceLikelihoodBuffer buffer = result.await();
(在 AsyncTask
中),以获得我目前最有可能所在的位置。
代码运行完美,但响应非常慢,尤其是我第一次打开我的应用程序和运行 search/method。
第一次通常需要3秒才能得到回应。所以我的问题是这是否正常?还是应该快得多?在那种情况下,我行动迟缓的可能原因是什么。 (ps。GoogleApiClient
连接速度非常快。PlaceDetection
连接 APIclient
时 运行 秒。)
代码:
这里是Api Client
:
mGoogleApiClient = new GoogleApiClient.Builder(activity)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Places.PLACE_DETECTION_API)
.addApi(Places.GEO_DATA_API)
.build();
mGoogleApiClient.blockingConnect();
这里是获取 Placelikelyhood
:
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
PlaceLikelihoodBuffer buffer = result.await();
您正在使用 mGoogleApiClient.blockingConnect()
,它必须在后台线程中 运行,并且您正在使用 PendingResult.await()
,它也必须在后台线程中 运行 .
似乎使用后台线程的额外开销可能是额外延迟的原因。
请注意,另一个原因可能是您的数据连接速度。
而且,return 结果需要多长时间的另一个原因是基于当前位置的结果中有多少项目。
我只是使用回调而不使用后台线程对其进行了测试,并使用不同的数据连接类型测试了首次启动行为(我也确保在启动之前将其从任务列表中删除)。
我用了 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();
然后在 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();
}
});
}
第一次测试,连接到我的 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
//........
下一次测试,我连接到我的 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
下次测试,断开 WiFi,连接到 LTE。
毫不奇怪,这花了一点时间:2.642 秒。
然而,令人惊讶的是,它返回的结果比连接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