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

问题描述:

我正在尝试使用Google Places API创建自定义地点选择器。在Google Place Picker控件中,当滚动地图时,引脚位置会更改,并且相应的附近位置将被提取到新引脚位置。 在iOS/Android Google Places API中,我只能看到一个API从设备当前位置获取附近的地方。如何使用Google Places API滚动地图时更新附近的地点?

有Google Places WebService API可以用来获取任何给定经纬度附近的地方,但它可能通过iOS/Android API?

我们如何使用iOS/Android Google Places API来获取地图已滚动到的特定坐标的附近地点?

您应该使用place picker来选择自定义位置。

以下代码将启动位置选取器。

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

使用此方法后对附近的地方

以米为单位设置你的SEARCH TYPE和半径。

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