如何使用 Android 的 MapView 对象在 Google map 中添加搜索栏?
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mapmain);
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
}
最佳答案
假设您有一个名为:mapSearchBox 的 EditText
mapSearchBox = (EditText) findViewById(R.id.search);
mapSearchBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
actionId == EditorInfo.IME_ACTION_GO ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mapSearchBox.getWindowToken(), 0);
new SearchClicked(mapSearchBox.getText().toString()).execute();
mapSearchBox.setText("", TextView.BufferType.EDITABLE);
return true;
}
return false;
}
});
然后您需要“SearchClicked”类来处理请求:
private class SearchClicked extends AsyncTask<Void, Void, Boolean> {
private String toSearch;
private Address address;
public SearchClicked(String toSearch) {
this.toSearch = toSearch;
}
@Override
protected Boolean doInBackground(Void... voids) {
try {
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.UK);
List<Address> results = geocoder.getFromLocationName(toSearch, 1);
if (results.size() == 0) {
return false;
}
address = results.get(0);
// Now do something with this GeoPoint:
GeoPoint p = new GeoPoint((int) (address.getLatitude() * 1E6), (int) (address.getLongitude() * 1E6))
} catch (Exception e) {
Log.e("", "Something went wrong: ", e);
return false;
}
return true;
}
}
关于android - 谷歌地图android中的搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6011457/