我的应用程序中有一个搜索EditText,我想通过自动完成功能从谷歌地图将所有地址添加到此EditText中。
我想要我该怎么做。 谢谢!
EditText adress = (EditText) findViewById(R.id.adress);
AutoCompleteTextView placeAutoSearch;
ArrayList<String> placesList;
ArrayAdapter<String> placesListAdatper;
String placesURL;
String PLACES_BASE_URL = "https://maps.googleapis.com/maps/api/place/autocomplete/json?";
placeAutoSearch.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)
{
placesList = new ArrayList<String>();
updateList(s.toString());
}
@Override
public void afterTextChanged(Editable s)
{
}
});
private void updateList(String place)
{
String parameter = "input=" + place + "&types=geocode&sensor=true&key=" + PLACES_API_KEY;
placesURL = PLACES_BASE_URL + parameter;
((MapForAddressActivity)controller).getPlacesList(placesURL);
}
public void updatePlacesList(ArrayList<PlacesNameEntity> list)
{
placesList.clear();
for (int i=0; i<list.size(); i++)
{
placesList.add(i, list.get(i).getDescription());
}
placesListAdatper = new ArrayAdapter<String>(getBaseActivity(), android.R.layout.simple_list_item_1, placesList)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
android.widget.TextView text = (android.widget.TextView) view.findViewById(android.R.id.text1);
return view;
}
};
placeAutoSearch.setAdapter(placesListAdatper);
placesListAdatper.notifyDataSetChanged();
}
public void getAddressLatLong(String addressString)
{
Geocoder coder = new Geocoder(getBaseActivity());
List<Address> address;
LatLng latLng = null;
try {
address = coder.getFromLocationName(addressString, 5);
if (address == null) {
}
Address location = address.get(0);
latLng = new LatLng(location.getLatitude(), location.getLongitude() );
userLatLng.setLat(location.getLatitude());
userLatLng.setLon(location.getLongitude());
mMap.getGoogleMap().clear();
mMap.drawMarker(latLng);
((MapForAddressActivity)controller).hideKeyboard();
} catch (IOException ex) {
ex.printStackTrace();
}
}
从服务器获取地点列表(谷歌地点API(
public void getPlacesList(String parameter)
{
((ServiceFactory) serviceFactory).getProviderService()
.GetPlacesNameList(parameter)
.enableRetry(false)
.enqueue(new ServiceCallback(this, this)
{
@Override
protected void onSuccess(Object response, int code)
{
((MapForAddressActivityView)view).updatePlacesList(((PlacesNameResponse) response).getList());
}
@Override
protected void onFailure(String errorMessage, int code) {
showToast(errorMessage);
}
});
}
您的改造接口:
@GET()
ServiceCall<PlacesNameResponse> GetPlacesNameList(@Url String url);