【问题标题】:How to resolve "Places API for Android does not seem to be enabled for your app"如何解决“您的应用似乎未启用 Places API for Android”
【发布时间】:2019-02-22 12:28:51
【问题描述】:

这个错误已经困扰了我一天多。我已经彻底搜索过,但没有一个答案为我提供解决方案。 我已经正确设置了 API 密钥,应用程序限制提供了包名称和 SHA1 密钥。 这是代码 清单.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">
    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality.
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <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">
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity android:name=".PlacePickerActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/.
        -->


        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">

        </activity>

    </application>

</manifest>

地图活动

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

PlacePickerActivity

public class PlacePickerActivity extends AppCompatActivity {

    int PLACE_PICKER_REQUEST=1;
    TextView tvPlace;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_place_picker);
        tvPlace=findViewById(R.id.tv_Place);
    }

    public void goPlacePicker(View view) {
        PlacePicker.IntentBuilder builder=new PlacePicker.IntentBuilder();
        try{
            Log.e("error","1");
            startActivityForResult(builder.build(PlacePickerActivity.this),PLACE_PICKER_REQUEST);

        }catch(GooglePlayServicesRepairableException e)
        {
            Log.e("Repairable",e.toString());

            e.printStackTrace();
        }
        catch(GooglePlayServicesNotAvailableException e){
            Log.e("Not Available",e.toString());
            e.printStackTrace();
        }

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == PLACE_PICKER_REQUEST){
            if(resultCode == RESULT_OK){
                Log.e("Activity","2");
                Place place=PlacePicker.getPlace(PlacePickerActivity.this,data);
                Log.e("PlacePicker","3");
                tvPlace.setText(place.getAddress());
            }
        }

    }
}

提前感谢任何可能的帮助..

【问题讨论】:

  • 您是否在 Google API 控制台中启用了 Place SDK for Android?

标签: android api google-maps android-manifest google-places


【解决方案1】:

注意:Google Play Services 版本的 Places SDK for Android(在 Google Play Services 16.0.0 中)自 2019 年 1 月 29 日起弃用,并将于 7 月 29 日关闭, 2019 年。Android 版 Places SDK 的新版本现已推出。我们建议尽快更新到新版本。详情请见migration guide

您可以在折旧通知下的this link 中查看上述消息。

Place API 已移至单独的库,而不是 Google Play 服务。所以你必须迁移到 New Place API 库。


您可以follow this migration guideline下面的代码来集成 Places SDK for Android。

1.在应用级 build.gradle 文件中添加此依赖项:
implementation 'com.google.android.libraries.places:places:1.0.0'

注意:您的应用项目的 minSdkVersion 应为 14 或更高

2。在 Activity 中初始化 Places。
Places.initialize(getApplicationContext(), YOUR_API_KEY);

3。当你想打开 PlaceAutocomplete Activity 时调用下面的函数

private void startAutocompleteActivity() {
        List<com.google.android.libraries.places.api.model.Place.Field> placeFields = new ArrayList<>(Arrays.asList(com.google.android.libraries.places.api.model.Place.Field.values()));
        List<TypeFilter> typeFilters = new ArrayList<>(Arrays.asList(TypeFilter.values()));
// Create a RectangularBounds object.
  RectangularBounds bounds = RectangularBounds.newInstance(
    new LatLng(-33.880490, 151.184363),
    new LatLng(-33.858754, 151.229596));
        Intent autocompleteIntent =
                new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, placeFields)
                        .setLocationBias(bounds)
                        .setTypeFilter(typeFilters.get(0))
                        .build(this);
        startActivityForResult(autocompleteIntent, 1001);
    }  

4.写下面的onActivityResult代码

/**
     * Override the activity's onActivityResult(), check the request code, and
     * do something with the returned place data (in this example it's place name and place ID).
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1001) {
            if (resultCode == RESULT_OK) {
                Place place = Autocomplete.getPlaceFromIntent(data);
                Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
            } else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
                // TODO: Handle the error.
                Status status = Autocomplete.getStatusFromIntent(data);
                Log.i(TAG, status.getStatusMessage());
            } else if (resultCode == RESULT_CANCELED) {
                // The user canceled the operation.
            }
        }
    }


You can check this Demo application for more detail


现在最重要的是,您必须启用计费才能使用适用于 Android 的 New Place API。

要使用 Places SDK for Android,您必须在所有 API 请求中包含 API key,并且您必须在每个项目中添加 enable billing

查看this link 了解更多信息和价格。

SKU:基本数据

基本类别中的字段包含在地方请求的基本费用中,不会产生任何额外费用。请求以下任何字段时会触发基本数据 SKU:ADDRESS、ID、LAT_LNG、NAME、OPENING_HOURS、PHOTO_METADATAS、PLUS_CODE、TYPES、USER_RATINGS_TOTAL、VIEWPORT。

您可以在上面给出的same link 中查看定价和其他 SKU。

【讨论】:

  • 常量 BOUNDS_MOUNTAIN_VIEW 和 SELECT_REQUEST_PLACE 有什么用??
  • 检查我的更新答案。 BOUNDS_MOUNTAIN_VIEW 设置为将自动完成结果偏向特定地理区域。您可以根据自己的情况删除。 SELECT_REQUEST_PLACE 是 startActivityForResult 的常量字段,因此我可以使用相同的值获取和比较结果。这里是 1001。
  • 对于自动完成,它显示 REQUEST_DENIED 错误。任何想法如何解决它?
  • 可能是您的 API 密钥错误。 Check this answer。您必须使用服务器密钥(由 Google 服务自动创建),该密钥将在您项目的 console.developers.google.com 中可用
  • 您也可以refer Troubleshooting section in this link 处理 REQUEST_DENIED 问题。
猜你喜欢
  • 2020-07-24
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
相关资源
最近更新 更多