Getting results of nearby places from User's Location using Google Maps API in Android

后端 未结 4 1642
别那么骄傲
别那么骄傲 2020-12-01 02:33

This is the first time using google map API and google places API. I am doing a demo application that displays the list hospitals (for example) nearest to the user\'s locati

4条回答
  •  醉酒成梦
    2020-12-01 02:54

    Follow below code:

    private static final String TAG_RESULT = "predictions";
    JSONObject json;
    
    
    ArrayList names;
    ArrayAdapter adapter;
    String browserKey = "Google API key";
    //------------------------AutoComplete code--------------  
      public void updateList(String place) {
            String input = "";
    
            try {
                input = "input=" + URLEncoder.encode(place, "utf-8");
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            }
    
            String output = "json";
            String parameter = input + "&types=geocode&sensor=true&key="                + browserKey;
    
            url = "https://maps.googleapis.com/maps/api/place/autocomplete/"                + output + "?" + parameter;
    
            JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url,
                    null, new Response.Listener() {
    
                @Override            public void onResponse(JSONObject response) {
                    System.out.println("response"+response);
                    try {
    
                        JSONArray ja = response.getJSONArray(TAG_RESULT);
    
                        for (int i = 0; i < ja.length(); i++) {
                            JSONObject c = ja.getJSONObject(i);
                            String description = c.getString("description");
                            System.out.println("description"+description);
                            names.add(description);
                        }
    
                        adapter = new ArrayAdapter(
                                getApplicationContext(),
                                android.R.layout.simple_list_item_1, names) {
                            @Override                        public View getView(int position,
                                                View convertView, ViewGroup parent) {
                                View view = super.getView(position,
                                        convertView, parent);
                                TextView text = (TextView) view
                                        .findViewById(android.R.id.text1);
                                text.setTextColor(Color.BLACK);
                                return view;
                            }
                        };
                        edittext_signup_city.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    } catch (Exception e) {
                    }
                }
            }, new Response.ErrorListener() {
                @Override            public void onErrorResponse(VolleyError error) {
                }
            });
            AppController.getInstance().addToRequestQueue(jsonObjReq, "jreq");
        }
    

提交回复
热议问题