2017-05-09 89 views
1

我正在嘗試使用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來獲取地圖已滾動到的特定座標的附近地點?

回答

-1

您應該使用place picker來選擇自定義位置。

以下代碼將啓動位置選取器。

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

使用此方法後對附近的地方

以米爲單位設置你的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); 
相關問題