在谷歌地图Android的搜索栏 [英] search bar in google map android

查看:135
本文介绍了在谷歌地图Android的搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在谷歌地图使用的是Android的MapView对象添加​​一个搜索栏?

 公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.mapmain);
    图形页面=(图形页面)findViewById(R.id.mapView);
    的LinearLayout zoomLayout =(的LinearLayout)findViewById(R.id.zoom);
    查看ZOOMVIEW = mapView.getZoomControls();    zoomLayout.addView(ZOOMVIEW,
        新LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    mapView.displayZoomControls(真);}


解决方案

假设你有一个叫做一个EditText:mapSearchBox

  mapSearchBox =(EditText上)findViewById(R.id.search);
    mapSearchBox.setOnEditorActionListener(新TextView.OnEditorActionListener(){
        公共布尔onEditorAction(TextView的V,INT actionId,KeyEvent的事件){
             如果(actionId == || EditorInfo.IME_ACTION_SEARCH
                    actionId == || EditorInfo.IME_ACTION_DONE
                    actionId == || EditorInfo.IME_ACTION_GO
                    event.getAction()== KeyEvent.ACTION_DOWN&放大器;&放大器;
                    event.getKey code()== KeyEvent.KEY code_ENTER){                //隐藏虚拟键盘
                InputMethodManager IMM =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(mapSearchBox.getWindowToken(),0);                新SearchClicked(mapSearchBox.getText()的toString()。)的execute()。
                mapSearchBox.setText(,TextView.BufferType.EDITABLE);
                返回true;
            }
            返回false;
        }
    });

然后,你需要的SearchClicked类来处理请求:

 私有类SearchClicked扩展的AsyncTask<太虚,太虚,布尔> {
        私人字符串toSearch;
        私有地址地址;        公共SearchClicked(字符串toSearch){
            this.toSearch = toSearch;
        }        @覆盖
        保护布尔doInBackground(虚空......空隙){            尝试{
                地理codeR地理codeR =新的地缘codeR(getApplicationContext(),Locale.UK);
                清单<地址>结果=地理coder.getFromLocationName(toSearch,1);                如果(results.size()== 0){
                    返回false;
                }                地址= results.get(0);                  //现在做这个的GeoPoint的东西:
                GeoPoint的P =新的GeoPoint((INT)(address.getLatitude()* 1E6),(INT)(address.getLongitude()* 1E6))            }赶上(例外五){
                Log.e(,出事了,E);
                返回false;
            }
            返回true;
        }
}

How can I add a search bar in Google map using MapView object of Android?

    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);

}

解决方案

Assuming you have an EditText called: mapSearchBox

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;
        }
    });

Then you need the 'SearchClicked' class to handle the request:

   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的搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆