我想在這個項目中添加地圖,你可以找到這個項目:只需鍵入谷歌(mobisystech/autocompleteview·GitHub)。我想實現從谷歌的自動完成地址place-api
如果用戶選擇任何地址,它應顯示在地圖上。請幫幫我。
我想在這個項目中添加地圖,你可以找到這個項目:只需鍵入谷歌(mobisystech/autocompleteview·GitHub)。我想實現從谷歌的自動完成地址place-api
如果用戶選擇任何地址,它應顯示在地圖上。請幫幫我。
使用此代碼獲取地址並將其保存在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());
}
}
我想顯示的地圖和地圖上我想顯示用戶所選地址 –
乘坐位置點從地址和顯示座標基於該點的地圖中的位置。 – Android007
代碼請....不要把鏈接與404 –