android - 谷歌地图android中的搜索栏

标签 android google-maps-api-2

如何使用 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/

相关文章:

java - 应用程序激活时重新加载数据

android - 如何测试同步适配器运行

java - 在 Activity 中显示谷歌地图(不是MainActivity)

html - 静态谷歌地图在 iOS7.1 上不显示

Android:初始音频处理方法调用需要很长时间

android - 在 android 中使用生命周期

java - Android中如何将文本插入到当前聚焦的文本框中

Android Maps API v2 - 在我的位置光标上单击事件

android - 如何使用 OnMarkerClickListener

google-maps - 如何更改 Google map 标记的颜色?