2016-03-15 21 views
0

我想使用谷歌API當我輸入關鍵字它必須給我的城市名稱與該關鍵字我想使用這通過使用StartActivityForResult() 這裏是我到目前爲止我的代碼:但我不知道接下來要做什麼,我有API密鑰,我已經用我的清單文件中的密鑰,我只是想完成編碼部分!我想在我的應用程序中使用谷歌API,我插入任何關鍵字,所以它必須autocompete,並給我城市地址

case R.id.linearPlace:{ startActivityForResult(new Intent(this,Location.class),CITY); 

      }break; 
     } 
    } 

    public void onActivityResult(int requestcode,int resultcode,Intent intent){ 
     super.onActivityResult(requestcode,resultcode,intent); 
     String geoName = intent.getStringExtra(""); 
    } 
+0

http://stackoverflow.com/questions/19398702/get-formatted-address-from -google-maps-api-json –

回答

0

在這裏我把我的代碼,以便在變化的EditText事件簡單的搜索地址

佈局文件:activity_demo.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp" 
    tools:context="com.skandinaviske.elevkalender.DemoActivity"> 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/actLocation" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:hint="Key word" /> 

     <EditText 
      android:id="@+id/edtCountryCode" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:hint="Country Name" /> 
    </LinearLayout> 

    <ListView 
     android:id="@+id/lstAddress" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/linearLayout2"></ListView> 

</RelativeLayout> 

類文件:DemoActivity.java

package com.demo.pack; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 

import com.android.volley.RequestQueue; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.VolleyLog; 
import com.android.volley.toolbox.JsonObjectRequest; 
import com.android.volley.toolbox.Volley; 

import org.json.JSONArray; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.HashMap; 

public class DemoActivity extends Activity { 
    private RequestQueue mRequestQueue; 
    private EditText actLocation; 
    public ArrayList<HashMap<String, String>> listofAddress; 
    private ListView lstAddress; 
    private EditText edtCountryCode; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_demo); 
     mRequestQueue = Volley.newRequestQueue(this); 
     actLocation = (EditText) findViewById(R.id.actLocation); 
     edtCountryCode = (EditText) findViewById(R.id.edtCountryCode); 
     lstAddress = (ListView) findViewById(R.id.lstAddress); 
     actLocation.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       if (actLocation.getText().toString().length() > 3) { 
        String url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + actLocation.getText().toString().trim() + "%20" + edtCountryCode.getText().toString() + "&key=API_KEY&language=en_us"; 
        fetchJsonResponse(url); 
       } 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 
    } 

    private void fetchJsonResponse(String s) { 
     // Pass second argument as "null" for GET requests 
     JsonObjectRequest req = new JsonObjectRequest(s, null, 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         //String result = "Your IP Address is " + response.getString("ip"); 
         Log.d("response==>", response.toString()); 
         calldisplayList(response); 
         //Toast.makeText(EditProfileActivity.this, response.toString(), Toast.LENGTH_SHORT).show(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.e("Error: ", error.getMessage() + ""); 
      } 
     }); 

     /* Add your Requests to the RequestQueue to execute */ 
     mRequestQueue.add(req); 
    } 

    private void calldisplayList(JSONObject response) { 
     String city = "", state = "", country = ""; 
     if (response != null) { 
      try { 
       if (response.getString("status").toString().equals("OK")) { 
        JSONArray adddressArray = response.getJSONArray("results"); 
        if (adddressArray.length() != 0) { 
         if (listofAddress == null) { 
          listofAddress = new ArrayList<>(); 
         } 
         listofAddress.clear(); 
         for (int i = 0; i < adddressArray.length(); i++) { 
          JSONObject jsonObject = adddressArray.getJSONObject(i); 
          HashMap<String, String> hashMap = new HashMap<>(); 
          hashMap.put("address", jsonObject.getString("formatted_address").toString()); 
          listofAddress.add(hashMap); 
         } 
         if (listofAddress.size() != 0 && actLocation.getText().length() != 0) { 
          AddressListAdapter addressListAdapter = new AddressListAdapter(DemoActivity.this, listofAddress); 
          lstAddress.setAdapter(addressListAdapter); 
          lstAddress.setVisibility(View.VISIBLE); 
         } else { 
          lstAddress.setVisibility(View.GONE); 
         } 
        } else { 

        } 
       } else { 

       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } else { 

     } 
    } 

    public class AddressListAdapter extends BaseAdapter { 
     private final Activity context; 
     private final ArrayList<HashMap<String, String>> listOfAddress; 

     private class ViewHolder { 
      TextView txtAddress; 
     } 

     public AddressListAdapter(Activity context1, ArrayList<HashMap<String, String>> listOfAddress) { 
      this.context = context1; 
      this.listOfAddress = listOfAddress; 
     } 


     @Override 
     public int getCount() { 
      return listOfAddress.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return listOfAddress.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View view, ViewGroup parent) { 
      ViewHolder viewHolder; 
      View converView = view; 
      if (converView == null) { 
       LayoutInflater infalInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       converView = infalInflater.inflate(R.layout.address_list_item, null); 
       viewHolder = new ViewHolder(); 
       viewHolder.txtAddress = (TextView) converView.findViewById(R.id.txtAddress); 
       converView.setTag(viewHolder); 
      } else { 
       viewHolder = (ViewHolder) converView.getTag(); 
      } 
      viewHolder.txtAddress.setText(listOfAddress.get(position).get("address") + ""); 

      return converView; 
     } 
    } 

} 

適配器的佈局文件:address_list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:id="@+id/txtAddress" 
     android:layout_width="match_parent" 
     android:background="@color/white" 
     android:textColor="@color/black" 
     android:text="temp data" 
     android:gravity="center_vertical" 
     android:layout_height="wrap_content" 
     android:padding="5dp" /> 
</LinearLayout> 

不要錯過把API_KEY(聽到的是從那裏你可以得到的API密鑰的網址:https://developers.google.com/maps/documentation/geocoding/get-api-key#get-an-api-key

+0

你能否給我提供一個演示代碼,比如我應該如何調用這個URL?我是新的,從來沒有使用谷歌API有史以來 – bipin

+0

好吧,沒問題,但給我一段時間。 –

+0

ohk非常感謝! – bipin

相關問題