如何在google maps v2中添加一个搜索栏,其中a)建议b)下拉菜单

时间:2015-10-16 06:12:21

标签: java android google-maps

我正在构建一个类似优步的出租车应用程序..我正在使用Android Studio并在Java中实现代码。服务器端的Java servlet和jsps,我的sql作为数据库。 任何带有我提到的要求的链接或代码都会有很大的帮助。

我想要一个带有建议的搜索栏,其中有一个下拉菜单,包括Uber应用程序中的拾取和下载位置。

提前致谢。

1 个答案:

答案 0 :(得分:0)

创建新活动并从用户那里获取输入或使用静态输入,以便从谷歌查询附近的地方

StringBuilder sb = new StringBuilder("https://maps.googleapis.com/maps/api/place/textsearch/json?");
sb.append("query=" + "Your input OR user input";
sb.append("&key=You Api key here");

PlacesTask placesTask = new PlacesTask();

placesTask.execute(sb.toString());

并创建一个PlacesTask

private class PlacesTask extends AsyncTask<String, Integer, String> {
        String data = null;

        @Override
        protected String doInBackground(String... url) {
            try {
                data = downloadUrl(url[0]);
            } catch (Exception e) {
                Log.d("Background Task", e.toString());
            }
            return data;
        }

        @Override
        protected void onPostExecute(String result) {
            ParserTask parserTask = new ParserTask();
            parserTask.execute(result);
        }

    }

private String downloadUrl(String strUrl) throws IOException {
        String data = "";
        InputStream iStream = null;
        HttpURLConnection urlConnection = null;
        try {
            URL url = new URL(strUrl);

            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();
            iStream = urlConnection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
            StringBuffer sb = new StringBuffer();

            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

            data = sb.toString();

            br.close();

        } catch (Exception e) {
            Log.d("Exception url", e.toString());
        } finally {
            iStream.close();
            urlConnection.disconnect();
        }
        return data;
    }

然后创建Paresetask

private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String, String>>> {

        JSONObject jObject;

        @Override
        protected List<HashMap<String, String>> doInBackground(String... jsonData) {

            List<HashMap<String, String>> places = null;
            PlacesJSONParser placeJsonParser = new PlacesJSONParser();

            try {
                jObject = new JSONObject(jsonData[0]);
                places = placeJsonParser.parse(jObject);

            } catch (Exception e) {
                Log.d("Exception", e.toString());
            }
            return places;
        }

        // Executed after the complete execution of doInBackground() method
        @Override
        protected void onPostExecute(List<HashMap<String, String>> list) {
            if (customSearch) {
                if (list.size() == 0) {
                    placesList.setVisibility(View.GONE);
                    error_text.setVisibility(View.VISIBLE);
                    error_text.setTextColor(getResources().getColor(R.color.error_message_color));
                } else {
                    placesList.setVisibility(View.VISIBLE);
                    error_text.setVisibility(View.GONE);
                }
                mPlaceArrayList = new ArrayList<>();
                mAdapter = new PlaceListAdapter(NearByPlaces.this, mPlaceArrayList, true);
                placesList.setAdapter(mAdapter);
            }
            for (int i = 0; i < list.size(); i++) {

                mNearByPlace = new NearByPlace();
                HashMap<String, String> hmPlace = list.get(i);
                mNearByPlace.setLatitude(hmPlace.get("lat"));
                mNearByPlace.setLongitude(hmPlace.get("lng"));
                mNearByPlace.setName(hmPlace.get("place_name"));
                mNearByPlace.setVicinity(hmPlace.get("vicinity"));
                mPlaceArrayList.add(mNearByPlace);

            }
            mAdapter.setPlaceArrayList(mPlaceArrayList);
        }

    }

和NearByPlace是

public class NearByPlace {

    private String name;
    private String address;
    private String latitude;
    private String longitude;
    private String vicinity;
    private String placeIcon;

    public NearByPlace(){}

    public String getPlaceIcon() {
        return placeIcon;
    }

    public void setPlaceIcon(String placeIcon) {
        this.placeIcon = placeIcon;
    }

    public String getVicinity() {

        return vicinity;
    }

    public void setVicinity(String vicinity) {
        this.vicinity = vicinity;
    }

    public String getLongitude() {

        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getLatitude() {

        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getAddress() {

        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

希望它能帮到你,你可以解决我从我的一个项目中复制和粘贴代码的小错误。