2015-10-21 38 views

回答

1

使用此代碼獲取地址並將其保存在ArrayList中。

private String ConvertPointToLocation(LatLng loca) { 
      String address = ""; 
       Geocoder geoCoder=new Geocoder(getBaseContext(), Locale.getDefault()); 

       try { 
        List<Address> addresses = geoCoder.getFromLocation(
        loca.latitude , 
        loca.longitude, 1); 

        if (addresses.size() > 0) { 
        for (int index = 0; 
       index < addresses.get(0).getMaxAddressLineIndex(); index++) 
         address += addresses.get(0).getAddressLine(index) + " "; 
        } 
       } 
       catch (IOException e) {   
        e.printStackTrace(); 
       }  

       return address; 
     } 

然後在layout.xml視圖中創建AutoCompleteEditText字段。併爲AutoCompleteEditText製作適配器。並在適配器中傳遞地址列表。

使用此從Google API獲取地址。

class GetPlaces extends AsyncTask<String, Void, ArrayList<String>> { 

ArrayAdapter<String> adapter; 
AutoCompleteTextView textView; 

public GetPlaces(ArrayAdapter<String> adapter2, 
     AutoCompleteTextView textView2) { 
    this.adapter = adapter2; 
    this.textView = textView2; 
} 

@Override 
protected ArrayList<String> doInBackground(String... args) { 

    ArrayList<String> predictionsArr = new ArrayList<String>(); 

    try { 

     URL googlePlaces = new URL(
     // URLEncoder.encode(url,"UTF-8"); 
       "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" 
         + URLEncoder.encode(args[0].toString(), "UTF-8") 
         + "&types=geocode&language=en&sensor=true&key=AIzaSyCL6Y7UPntVeUHoK7W9z-Dx_ogY3OFjxKg"); 
     URLConnection tc = googlePlaces.openConnection(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
       tc.getInputStream())); 

     String line; 
     StringBuffer sb = new StringBuffer(); 
     // take Google's legible JSON and turn it into one big string. 
     while ((line = in.readLine()) != null) { 
      sb.append(line); 
     } 
     // turn that string into a JSON object 
     JSONObject predictions = new JSONObject(sb.toString()); 
     // now get the JSON array that's inside that object 
     JSONArray ja = new JSONArray(predictions.getString("predictions")); 

     for (int i = 0; i < ja.length(); i++) { 
      JSONObject jo = (JSONObject) ja.get(i); 
      // add each entry to our array 
      predictionsArr.add(jo.getString("description")); 
     } 
    } catch (IOException e) { 

     Log.e("YourApp", "GetPlaces : doInBackground", e); 

    } catch (JSONException e) { 

     Log.e("YourApp", "GetPlaces : doInBackground", e); 

    } 

    return predictionsArr; 

} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
} 

// then our post 
@Override 
protected void onPostExecute(ArrayList<String> result) { 

    Log.d("YourApp", "onPostExecute : " + result.size()); 
    // update the adapter 
    //adapter.setNotifyOnChange(true); 
    // attach the adapter to textview 
    //textView.setAdapter(adapter); 

    for (String string : result) { 

     Log.d("YourApp", "onPostExecute : result = " + string); 
     adapter.add(string); 
     //adapter.notifyDataSetChanged(); 

    } 
    adapter.notifyDataSetChanged(); 
    Log.d("YourApp", 
      "onPostExecute : autoCompleteAdapter" + adapter.getCount()); 

} 

}

+0

我想顯示的地圖和地圖上我想顯示用戶所選地址 –

+0

乘坐位置點從地址和顯示座標基於該點的地圖中的位置。 – Android007

相關問題