[英]You have exceeded your daily request quota for this API. Google Places API in Android apps
[英]Google Places API. PlaceLikelihoodBuffer slow response. Android
我正在使用PlaceLikelihoodBuffer buffer = result.await();
(在AsyncTask
),以获取当前我最可能位于的位置。
该代码可以正常工作,但是响应速度非常慢, 尤其是在我第一次打开应用程序并运行search / method时 。
第一次通常需要3秒钟才能得到回应。 所以我的问题是这是否正常? 还是应该更快? 在那种情况下,我迟钝的可能原因是什么。 ( 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()
,它也必须在后台线程中运行。
似乎使用后台线程的额外开销可能是造成额外延迟的原因。
请注意,另一个原因可能是数据连接的速度。
而且,返回结果需要多长时间的另一个原因是,基于当前位置,结果中有多少个项目。
我只是使用回调函数而不使用后台线程对其进行了测试,并使用不同的数据连接类型测试了首次启动行为(我确保在启动之前也从任务列表中将其杀死)。
我用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.