SearchBar中的SearchView for Maps onQueryTextSubmit

时间:2014-06-03 20:14:56

标签: android maps

我想在操作栏中设置一个搜索视图,在地图上设置标记。

我已经使用edittext(使用wptrafficanalyzer.in中的JSON教程的教程)完成了它,但搜索视图更原生,但我没有从搜索视图获取结果到我的位置onQueryTextSubmit。 GetText不起作用,但getQuery希望返回false,导致NullPointerException。

我希望有人可以帮助我: - )

这是我的代码:

    // Getting reference to SearchView

    search=(SearchView) findViewById(R.id.action_search);

    search.setOnQueryTextListener (new OnQueryTextListener() {
    // Setting click event listener for the find button


        @Override
        public boolean onQueryTextSubmit(String query){
            // Getting the place entered
            String location = search.getText().toString();

            if(location==null || location.equals("")){
                Toast.makeText(getBaseContext(), "Enter a location", Toast.LENGTH_SHORT).show();
                return false;
            }

            String url = "https://maps.googleapis.com/maps/api/geocode/json?";

            try {
                // encoding special characters like space in the user input place
                location = URLEncoder.encode(location, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            String address = "address=" + location;

            String sensor = "sensor=false";

            // url , from where the geocoding data is fetched
            url = url + address + "&" + sensor;

            // Instantiating DownloadTask to get places from Google Geocoding service
            // in a non-ui thread
            DownloadTask downloadTask = new DownloadTask();

            // Start downloading the geocoding places
            downloadTask.execute(url);
        }

        @Override
        public boolean onQueryTextChange(String arg0) {
            // TODO Auto-generated method stub
            return false;
        }
    });
}

1 个答案:

答案 0 :(得分:0)

我自己修正了: - )

我不得不将SearchView查询内容放到OnCreateOptionsMenu并放入"查询"作为返回值,所以它是" String location = query;"对于那些有兴趣的人