简体   繁体   English

使用Google Places API滚动地图时如何更新附近的地点?

[英]How to update nearby places when map is scrolled using Google Places API?

I am trying to create a custom place picker using Google Places API. 我正在尝试使用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. 在Google Place Picker控件中滚动地图时,引脚位置会发生变化,并且会获取相应的新引脚位置附近的位置。 In the iOS/Android Google Places API, I only see an API to fetch nearby places from the devices current location. 在iOS / Android Google Places API中,我只看到一个API,用于从设备当前位置获取附近的位置。

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? Google Places WebService API可用于获取任何给定纬度/经度的附近地点,但是可以通过iOS / Android API吗?

How can we fetch nearby places for a specific coordinate that a map has been scrolled to using the iOS/Android Google Places API? 我们如何使用iOS / Android Google Places API获取地图滚动到的特定坐标的附近位置?

Use this post method for nearby places 将此post方法用于附近的地方

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM