【问题标题】:Geo Data API not returning any results地理数据 API 未返回任何结果
【发布时间】:2017-09-05 05:56:42
【问题描述】:

我试图通过将 place_id 传递给 Places.GeoDataApi 来获取位置,但它没有返回任何内容,我确信 place_id 是正确的。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    String placeId = "ChIJja4QaeH0GjkRKK5cBjTfKQo";

    Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)
            .setResultCallback(new ResultCallback<PlaceBuffer>() {
                @Override
                public void onResult(@NonNull PlaceBuffer places) {
                    if(places.getStatus().isSuccess() && places.getCount()>0){
                        final Place myPlace = places.get(0);
                        Log.i(String.valueOf(MainActivity.this),
                                "Place found: " + myPlace.getName() + "\n Address: " + myPlace.getAddress());
                    } else {
                        Log.i(String.valueOf(MainActivity.this),"Place not found!");
                    }
                    places.release();
                }
            });
}

编辑:添加 manifest.xml 文件

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="MY_API_KEY"/>

</application>

【问题讨论】:

  • 发布您的清单...
  • 张贴@rafsanahmad007
  • @GautamHans 不建议向公众分享您的 API 密钥,请尽快隐藏...!!!!

标签: android google-places-api


【解决方案1】:

代码与我实现的非常相似..除了一件事。

你忘了打电话给mGoogleApiClient.connect();

在您的代码中:

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

mGoogleApiClient.connect(); //add this

//now call the placeID codes Result callback

编辑

对条件的轻微更新:

 if(places.getStatus().isSuccess()){
    final Place myPlace = places.get(0);
    Log.i(String.valueOf(MainActivity.this),
            "Place found: " + myPlace.getName() + "\n Address: " + myPlace.getAddress());
} else {
    Log.i(String.valueOf(MainActivity.this),"Place not found!");
}

【讨论】:

  • 谢谢。有效! :) 但是即使 place_id 正确,我也会在日志中找到 Place Not Found。
  • 是的,我正在指定 placeId,例如 String placeId = "ChIJja4QaeH0GjkRKK5cBjTfKQo";即使那样,也找不到这个地方。我也尝试了另一个 ID “ChIJfapwfcT0GjkR6aj2WIaRdIk”,它应该返回“Virk Hospital and Maternity Home”,但它会转到其他部分。
  • 我尝试了您推荐的方法,但结果仍然相同。不知道发生了什么。
  • 检查logcat.like中是否有错误或访问被拒绝..并清理构建项目..
  • 启用 Google Places API ...请参阅:stackoverflow.com/questions/31449649/…
猜你喜欢
  • 1970-01-01
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 2018-11-03
  • 1970-01-01
  • 2014-12-14
相关资源
最近更新 更多