谷歌将Api放置在Android应用程序中不起作用
我曾使用android的自动完成功能获取位置,但现在我刚刚创建了新的play console应用程序,因为我没有获得用于位置的android Sdk选项,而只有用于位置Api的选项,但我使用的代码与早期的places Autocomplete相同,我得到了错误: 获取位置预测时出错: 状态{statusCode=PLACES\u API\u ACCESS\u NOT\u CONFIGURED,resolution=null} 我正在使用以下gradle:谷歌将Api放置在Android应用程序中不起作用,android,google-maps-api-3,google-places-api,googleplacesautocomplete,Android,Google Maps Api 3,Google Places Api,Googleplacesautocomplete,我曾使用android的自动完成功能获取位置,但现在我刚刚创建了新的play console应用程序,因为我没有获得用于位置的android Sdk选项,而只有用于位置Api的选项,但我使用的代码与早期的places Autocomplete相同,我得到了错误: 获取位置预测时出错: 状态{statusCode=PLACES\u API\u ACCESS\u NOT\u CONFIGURED,resolution=null} 我正在使用以下gradle: implementation 'co
implementation 'com.google.android.gms:play-services-plus:16.0.0'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.android.gms:play-services-places:16.0.0'
我的位置适配器代码是:
public class PlaceArrayAdapter extends ArrayAdapter<PlaceArrayAdapter.PlaceAutocomplete> implements Filterable {
private static final String TAG = "PlaceArrayAdapter";
private GoogleApiClient mGoogleApiClient;
private AutocompleteFilter mPlaceFilter;
private LatLngBounds mBounds;
private ArrayList<PlaceAutocomplete> mResultList;
/**
* Constructor
*
* @param context Context
* @param resource Layout resource
* @param bounds Used to specify the search bounds
* @param filter Used to specify place types
*/
public PlaceArrayAdapter(Context context, int resource, LatLngBounds bounds,
AutocompleteFilter filter) {
super(context, resource);
mBounds = bounds;
mPlaceFilter = filter;
}
public void setGoogleApiClient(GoogleApiClient googleApiClient) {
if (googleApiClient == null || !googleApiClient.isConnected()) {
mGoogleApiClient = null;
} else {
mGoogleApiClient = googleApiClient;
}
}
@Override
public int getCount() {
return mResultList.size();
}
@Override
public PlaceAutocomplete getItem(int position) {
return mResultList.get(position);
}
private ArrayList<PlaceAutocomplete> getPredictions(CharSequence constraint) {
if (mGoogleApiClient != null) {
Logger.showMessage("Executing autocomplete query for: " + constraint);
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi
.getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
mBounds, mPlaceFilter);
// Wait for predictions, set the timeout.
AutocompletePredictionBuffer autocompletePredictions = results
.await(60, TimeUnit.SECONDS);
final Status status = autocompletePredictions.getStatus();
if (!status.isSuccess()) {
Toast.makeText(getContext(), "Error: " + status.toString(),
Toast.LENGTH_SHORT).show();
Logger.showMessage("Error getting place predictions: " + status
.toString());
autocompletePredictions.release();
return null;
}
Logger.showMessage("Query completed. Received " + autocompletePredictions.getCount()
+ " predictions.");
Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
while (iterator.hasNext()) {
AutocompletePrediction prediction = iterator.next();
resultList.add(new PlaceAutocomplete(prediction.getPlaceId(),
prediction.getFullText(null)));
}
// Buffer release
autocompletePredictions.release();
return resultList;
}
Logger.showMessage("Google API client is not connected.");
return null;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null) {
// Query the autocomplete API for the entered constraint
mResultList = getPredictions(constraint);
if (mResultList != null) {
// Results
results.values = mResultList;
results.count = mResultList.size();
}
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
// The API returned at least one result, update the data.
notifyDataSetChanged();
} else {
// The API did not return any results, invalidate the data set.
notifyDataSetInvalidated();
}
}
};
return filter;
}
public class PlaceAutocomplete {
public CharSequence placeId;
public CharSequence description;
PlaceAutocomplete(CharSequence placeId, CharSequence description) {
this.placeId = placeId;
this.description = description;
}
@Override
public String toString() {
return description.toString();
}
}
}
公共类PlaceArrayAdapter扩展ArrayAdapter实现可过滤{
私有静态最终字符串TAG=“PlaceArrayAdapter”;
私人GoogleapClient MGoogleapClient;
专用自动完成过滤器;
私人LatLngBounds mBounds;
私有ArrayList mResultList;
/**
*建造师
*
*@param上下文
*@param资源布局资源
*@param bounds用于指定搜索边界
*@param筛选器用于指定位置类型
*/
public PlaceArrayAdapter(上下文、int资源、LatLngBounds边界、,
自动完成过滤器(过滤器){
超级(上下文、资源);
mBounds=边界;
mPlaceFilter=过滤器;
}
公共无效设置GoogleAppClient(GoogleAppClient GoogleAppClient){
if(googleApiClient==null | |!googleApiClient.isConnected()){
mgoogleapclient=null;
}否则{
mGoogleApiClient=googleApiClient;
}
}
@凌驾
public int getCount(){
返回mResultList.size();
}
@凌驾
公共场所自动完成getItem(内部位置){
返回mResultList.get(位置);
}
私有ArrayList getPredictions(CharSequence约束){
if(mGoogleApiClient!=null){
showMessage(“对“+约束”执行自动完成查询);
Pendingreult结果=
Places.GeoDataApi
.getAutocompletePredictions(mgoogleAppClient,constraint.toString(),
mBounds,mPlaceFilter);
//等待预测,设置超时。
AutocompletePredictionBuffer autocompletePredictions=结果
.等待(60,时间单位秒);
最终状态状态=自动完成预测。getStatus();
如果(!status.issucess()){
Toast.makeText(getContext(),“错误:+status.toString(),
吐司。长度(短)。show();
Logger.showMessage(“获取位置预测时出错:”+状态
.toString());
autocompletePredictions.release();
返回null;
}
Logger.showMessage(“查询已完成。已接收”+autocompletedpredictions.getCount()
+"预测";;
迭代器迭代器=自动完成预测。迭代器();
ArrayList resultList=新的ArrayList(autocompletePredictions.getCount());
while(iterator.hasNext()){
AutocompletePrediction prediction=iterator.next();
resultList.add(新建PlaceAutocomplete(prediction.getPlaceId()),
getFullText(null));
}
//缓冲释放
autocompletePredictions.release();
返回结果列表;
}
Logger.showMessage(“谷歌API客户端未连接”);
返回null;
}
@凌驾
公共过滤器getFilter(){
过滤器过滤器=新过滤器(){
@凌驾
受保护的筛选器结果性能筛选(CharSequence约束){
FilterResults results=新的FilterResults();
if(约束!=null){
//查询输入约束的自动完成API
mResultList=getPredictions(约束);
if(mResultList!=null){
//结果
results.values=mResultList;
results.count=mResultList.size();
}
}
返回结果;
}
@凌驾
受保护的void publishResults(CharSequence约束、FilterResults结果){
if(results!=null&&results.count>0){
//API返回了至少一个结果,请更新数据。
notifyDataSetChanged();
}否则{
//API未返回任何结果,因此数据集无效。
notifyDataSetionValidated();
}
}
};
回流过滤器;
}
公共类PlaceAutocomplete{
公共字符序列;
公共字符序列描述;
PlaceAutocomplete(字符序列placeId,字符序列描述){
this.placeId=placeId;
this.description=描述;
}
@凌驾
公共字符串toString(){
返回description.toString();
}
}
}
活动代码:
GoogleApiClient mGoogleApiClient;
GoogleSignInOptions gso;
private PlaceArrayAdapter mPlaceArrayAdapter;
private void googlePlaceApi() {
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.enableAutoManage(this, MYPerference.GOOGLE_API_CLIENT_ID, this)
.addConnectionCallbacks(this)
.build();
etLocation.setThreshold(1);
etLocation.setOnItemClickListener(mAutocompleteClickListener);
mPlaceArrayAdapter = new PlaceArrayAdapter(this, android.R.layout.simple_list_item_1,
BOUNDS_MOUNTAIN_VIEW, null);
etLocation.setAdapter(mPlaceArrayAdapter);
}
private AdapterView.OnItemClickListener mAutocompleteClickListener
= new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final PlaceArrayAdapter.PlaceAutocomplete item = mPlaceArrayAdapter.getItem(position);
Logger.showMessage(" place id : "+item.placeId);
final String placeId = String.valueOf(item.placeId);
PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
.getPlaceById(mGoogleApiClient, placeId);
placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
}
};
private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
= new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if (!places.getStatus().isSuccess()) {
return;
}
Logger.showMessage(" error : "+places.getStatus().getStatusMessage());
Logger.showMessage(" error : "+places.getStatus());
// Selecting the first object buffer.
final Place place = places.get(0);
CharSequence attributions = places.getAttributions();
address = String.valueOf(place.getAddress());
latString = String.valueOf(place.getLatLng().latitude);
lngString = String.valueOf(place.getLatLng().longitude);
//getLatLongFromAddress(address);
}
};
GoogleApiClient-mGoogleApiClient;
谷歌签名gso;
私人配售ArrayAdapter mPlaceArrayAdapter;
私有void googlePlaceApi(){
gso=新建GoogleSignionOptions.Builder(GoogleSignionOptions.DEFAULT\u登录)
.requestEmail()
.build();
mgoogleapclient=新的Googleapclient.Builder(此)
.addApi(Places.GEO_数据_API)
.addApi(Auth.GOOGLE\u SIGN\u IN\u API,gso)
.enableAutoManage(这个,MYPerference.GOOGLE\u API\u CLIENT\u ID,这个)
.addConnectionCallbacks(此)
.build();
etLocation.setThreshold(1);
setLocation.setOnItemClickListener(mAutocompleteClickListener);
mPlaceArrayAdapter=new PlaceArrayAdapter(这是android.R.layout.simp