1
votes

I am trying to create a custom place picker using Google Places API. In the Google Place Picker control when the map is scrolled, the pin location changes and the corresponding nearby places to the new pin position are fetched. In the iOS/Android Google Places API, I only see an API to fetch nearby places from the devices current location.

There is Google Places WebService API which can be used to fetch nearby places for any given lat/long, but is it possible through the iOS/Android APIs?

How can we fetch nearby places for a specific coordinate that a map has been scrolled to using the iOS/Android Google Places API?

2

2 Answers

1
votes

Use this post method for nearby places

set your SEARCH TYPE and radius in meters.

String url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json" + "?location=" + location.getLatitude() + "," + location.getLongitude() + "&radius=" + RADIUS_IN_METERS + "&type=" + SEARCH_TYPE + "&key=" + API_KEY;
Log.d(TAG, url);
Map<String, String> params = new HashMap<>();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(params),
    new Response.Listener<JSONObject>() {        
    @Override
    public void onResponse(JSONObject response) {
    try {
            locationList = new ArrayList<>();
            JSONArray results = response.getJSONArray("results");
            for (int i = 0; i < results.length(); i++) {
                    JSONObject geometry = results.getJSONObject(i).getJSONObject("geometry");
                    String name = results.getJSONObject(i).getString("name");
                    String address = results.getJSONObject(i).getString("vicinity");
                    String place_id = results.getJSONObject(i).getString("place_id");
                    if (geometry != null) {
                            JSONObject location = geometry.getJSONObject("location");
                            if (location != null) {
                                    double latitude = location.getDouble("lat");
                                    double longitude = location.getDouble("lng");
                                    if (latitude != 0.0 && longitude != 0.0) {
                                            Location markerLocation = new Location(SEARCH_TYPE);
                                            markerLocation.setLatitude(latitude);
                                            markerLocation.setLongitude(longitude);
                                            Bundle bundle = new Bundle();
                                            bundle.putString("NAME", name);
                                            bundle.putString("ADDRESS", address);
                                            bundle.putString("PLACE_ID", place_id);
                                            bundle.putString("TYPE", SEARCH_TYPE);
                                            markerLocation.setExtras(bundle);
                                            locationList.add(markerLocation);
                                            Log.d(TAG, latitude + " " + longitude);
                                        }
                                    }
                                }

                            }
                            if (locationList.size() != 0) {
                                addMarkers(SEARCH_TYPE);
                            }
                            //Log.d(TAG,results.toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );
    requestQueue.add(jsonObjectRequest);
-1
votes

You should be using the place picker to choose a custom location.

The following code launches the place picker.

int PLACE_PICKER_REQUEST = 1;
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);