我正在使用Google Map API让用户选择位置。地图工作正常。获取位置名称的自动完成建议时出错。我已经在Google API控制台中检查了配置。没关系。
错误代码为9003,places_api_access_not_configured
完整的错误消息是: 联系API时出错: com.google.android.gms.tasks.RuntimeExcecutionException: com.google.android.gms.common.api.ApiException:1009: places_api_access_not_configured
build.gradle(模块)为
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "package.name"
minSdkVersion 16
targetSdkVersion 26
versionCode 11
versionName "3.0.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
useLibrary 'org.apache.http.legacy'
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//noinspection GradleCompatible,GradleCompatible
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//noinspection GradleCompatible,GradleCompatible
implementation 'com.android.support:cardview-v7:21.+'
implementation 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'com.facebook.shimmer:shimmer:0.1.0@aar'
implementation 'net.cachapa.expandablelayout:expandablelayout:2.9.2'
implementation 'com.google.code.gson:gson:2.8.1'
implementation 'com.squareup.picasso:picasso:2.4.0'
//google map
implementation 'com.google.android.gms:play-services:11.4.0'
}
configurations.all {
resolutionStrategy.force 'com.android.support:support-
annotations:27.1.1'
}
apply plugin: 'com.google.gms.google-services'
build.gradle(项目)为
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
//classpath 'com.google.gms:google-services:4.+'
classpath 'com.google.gms:google-services:4.0.1'
}
}
allprojects {
repositories {
google()
jcenter()
maven{
url "https://maven.google.com"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
关于地图活动的代码以获取自动完成建议
private AdapterView.OnItemClickListener mAutocompleteClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//in.hideSoftInputFromWindow(view.getWindowToken(), 0);
in.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
final AutocompletePrediction item = mPlaceAutocompleteAdapter.getItem(i);
final String placeID = item.getPlaceId();
PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
.getPlaceById(mGoogleApiClient, placeID);
placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
}
};
private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback = new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(@NonNull PlaceBuffer places) {
hideSoftKeyBoard();
if (!places.getStatus().isSuccess()) {
Log.d(TAG, "onResult: Place query did not complete successfully!" + places.getStatus().toString());
places.release();
return;
}
final Place place = places.get(0);
try {
mPlace = new PlaceInfo();
mPlace.setName(place.getName().toString());
mPlace.setAddress(place.getAddress().toString());
//mPlace.setAttributions(place.getAttributions().toString());
mPlace.setId(place.getId());
mPlace.setLatLng(place.getLatLng());
mPlace.setRating(place.getRating());
mPlace.setPhoneNumber(place.getPhoneNumber().toString());
mPlace.setWebsiteUri(place.getWebsiteUri());
LatLng latLng = place.getLatLng();
LattitudeToRetun = String.valueOf(latLng.latitude);
LongitudeToRetun = String.valueOf(latLng.longitude);
getAddressFromLatLong(latLng.latitude, latLng.longitude);
Log.d(TAG, "onResult: Place details : " + mPlace.toString());
} catch (NullPointerException e) {
Log.e(TAG, "onResult: NullPointerException : " + e.getMessage());
}
//moveCamera(mPlace.getLatLng(), DEFAULT_ZOOM, mPlace.getName());
moveCamera(new LatLng(place.getViewport().getCenter().latitude, place.getViewport().getCenter().longitude)
, DEFAULT_ZOOM, mPlace);
places.release();
}
};
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="package.name">
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:hardwareAccelerated="false"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@drawable/logo_500"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_map_API_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<service android:name=".Utils.MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".Utils.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<receiver
android:name=".MyBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.igkv.customhiring.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<activity
android:name=".Activity.DefaultActivity"
android:launchMode="singleInstance"
android:theme="@style/MyTheme.NoActionBar"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>