【问题标题】:Unable to add Place Autocomplete in android app无法在 Android 应用中添加地点自动完成功能
【发布时间】:2019-09-14 20:25:21
【问题描述】:

我的场景

我正在构建一个使用 Google 地图的 android 应用程序。

我正在使用以 Json 形式为我提供谷歌位置名称的 API 和 我在显示位置提示的适配器上添加了该名称。 我已经在应用程序中实现了 Place AutoComplete 来给我 当我输入“I”时,自动提示,例如印度、印多尔等。

问题陈述

我的 Json 数组列表返回 NULL,但它应该返回值列表 带有自动建议。

这是我的代码

whitebord.setThreshold(0);
names=new ArrayList<String>();
whitebord.addTextChangedListener(new TextWatcher()
{

 public void afterTextChanged(Editable s)
{

}

 public void beforeTextChanged(CharSequence s, int start,
 int count, int after)
 {

  }

  public void onTextChanged(CharSequence s, int start,
  int before, int count)
  {


  search_text= whitebord.getText().toString().split(",");
  url="https://maps.googleapis.com/maps/api/place/autocomplete/json?  
  input="+search_text[0]+"&sensor=true&key="+browserKey;
  if(search_text.length<=1){
 names=new ArrayList<String>();
 Log.d("URL",url);
  paserdata parse=new paserdata();
  parse.execute();

 }
  }
 });

public class paserdata extends AsyncTask<Void, Integer, Void>{

@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub


 JSONParser jParser = new JSONParser();

// getting JSON string from URL
 json = jParser.getJSONFromUrl(url.toString());
if(json !=null)
 {
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_RESULT);

 for(int i = 0; i < contacts.length(); i++){
 JSONObject c = contacts.getJSONObject(i);
String description = c.getString("description");
Log.d("description", description);
 names.add(description);


}
} catch (JSONException e) {
 e.printStackTrace();

 }
 }

return null;
 }  


@Override
protected void onPostExecute(Void result) {
adp = new ArrayAdapter<String>(getApplicationContext(),
 android.R.layout.simple_list_item_1, names) {
  @Override
public View getView(int position, View convertView, ViewGroup parent) {
 View view = super.getView(position, convertView, parent);
 TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setTextColor(Color.BLACK);
return view;
 }
 };

  Toast.makeText(getApplicationContext(), result+"",
  Toast.LENGTH_LONG).show();
 whitebord.setAdapter(adp);


 }
 }

【问题讨论】:

标签: android google-maps autocomplete location


【解决方案1】:

从您的代码中,您已经构建了一个请求 url,但您没有使用任何 HttpURLConnection 来发出 API 请求。此外,您需要确保在您的 android 清单文件中添加互联网连接权限。

【讨论】:

  • 如何进行httpUrlconnection请求
【解决方案2】:

您的代码是正确的,但是如果您没有将您的帐户注册为结算帐户,您将无法使用该 api。看看:https://console.cloud.google.com/billing?pli=1

【讨论】:

    猜你喜欢
    • 2018-04-04
    • 1970-01-01
    • 2011-12-13
    • 2014-09-28
    • 2023-03-24
    • 2016-09-29
    • 2016-06-30
    • 1970-01-01
    • 2015-05-01
    相关资源
    最近更新 更多