0
我正在嘗試使用Google自動完成功能的新Google Places API,並且我編寫了一個測試應用程序來查看是否可以返回一些結果並正確解析它們。曼哈頓沒有「酒吧」!?!? Google Places Android API沒有按預期返回結果
在下面的示例中,我只是硬編碼LatLngs和基本上在曼哈頓,紐約地區的「酒吧」的查詢。但是,它始終需要很長時間才能完成(即使我處於40Mbps WiFi連接的情況下,仍然可以完成30秒以上),並且不會返回任何結果,只要我可以從日誌記錄中看出結果並從調試器運行此結果。
我沒有收到任何錯誤,並且據我所知我已經正確配置Places API以與我的測試應用程序一起使用。
任何人都知道我在這裏做什麼可能是錯的?
測試程序:
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();
}
}
清單:
<?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>
曼哈頓沒有酒吧確實是一個問題......嚴重的是,你從Google得到什麼迴應? – shkschneider
獲得0結果 05-07 16:38:19.195 8128-8128/com.googleplacestest我/標記:已連接 05-07 16:38:19.195 8128-8128/com.googleplacestest我/標記:LatLngBounds中心= 40.776572099999996,-73.94520415 05-07 16:38:19.195 8128-8169/com.googleplacestest我/標記:獲得「餐廳」的結果 05-07 16:39:01.635 8128-8169/com.googleplacestest我/標記:完成獲取結果 – ethan123
什麼是http響應?你確定你的查詢字符串格式正確嗎?並且,僅供參考 - 目前API的地點限於[60個響應](http://stackoverflow.com/questions/9614258/how-to-get-20-result-from-google-places-api)。 – wahwahwah