我已经完成了可以搜索的所有解决方案。我启用了与我的项目相关的API并且慢慢地制作了API密钥(浏览器,android),当然但没有一个正在运行。根据{{3}},我的API密钥格式错误或缺失。有没有办法在我运行项目时检查清单中的值是变化还是变为空?
答案 0 :(得分:1)
如果您已完成Google安装程序,那么只需尝试使用此网址,它一直在为我工作......
private String getPlacesApiUrl() {
String url = "";
String location = String.valueOf(currentLocation.latitude)+","+String.valueOf(currentLocation.longitude);
String radius = "5000";
String type = "hospital";
String sensor = "false";
String key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
try {
url = "https://maps.googleapis.com/maps/api/place/search/json"
+ "?location=" + URLEncoder.encode(location, "UTF-8")
+ "&type=" + URLEncoder.encode(type, "UTF-8")
+ "&radius=" + URLEncoder.encode(radius, "UTF-8")
+ "&sensor=" + URLEncoder.encode(sensor, "UTF-8")
+ "&key=" + URLEncoder.encode(key, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Log.d("url",url);
return url;
}
但请确保您使用的是浏览器密钥
祝你好运。
答案 1 :(得分:0)
我通过使用从教程中找到的代码解决了这个问题。希望这对有同样麻烦的人有所帮助。
我制作了PlaceAPI课程
class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
ArrayList<String> resultList;
Context mContext;
int mResource;
PlaceAPI mPlaceAPI = new PlaceAPI();
public PlacesAutoCompleteAdapter(Context context, int resource) {
super(context, resource);
mContext = context;
mResource = resource;
}
@Override
public int getCount() {
// Last item will be the footer
return resultList.size();
}
@Override
public String getItem(int position) {
return resultList.get(position);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
resultList = mPlaceAPI.autocomplete(constraint.toString());
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
}
并制作了一个适配器PlacesAutoCompleteAdapter
autocomplete = (AutoCompleteTextView) findViewById(R.id.autocomplete);
autocomplete.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.autocomplete_list_item));
autocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get data associated with the specified position
// in the list (AdapterView)
place = (String) parent.getItemAtPosition(position);
//Toast.makeText(this, description, Toast.LENGTH_SHORT).show();
}
});
然后在自动完成中使用适配器:
split