Google Places API:如何从纬度和经度中获取照片并放置ID?

我想获取当前位置的Google Place Details API所需的照片和place_id。

附近的搜索不会返回我确切位置的地方。 (android位置服务返回的当前lat / lng)。

雷达搜索需要关键字。 请建议。

根据Google商家搜索文档,您需要提供的三件事是KEY,LOCATION和RADIUS。 我删除了一堆不必要的代码,但这是我做类似的事情。

1)获取您当前的位置

private void initializeMapLocation() { LocationManager locationManager = (LocationManager) this .getSystemService(Context.LOCATION_SERVICE); Location lastLocation = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); if (lastLocation != null) { setUserLocation(lastLocation); } } private void setUserLocation(Location location) { LatLng currentLatLng = new LatLng(location.getLatitude(), location.getLongitude()); mMap.animateCamera(CameraUpdateFactory.newLatLng(currentLatLng)); } 

2)构建您的搜索URL。 您可以通过附加它们来添加额外参数(如关键字),但在这种特殊情况下听起来并不像您想要的那样。

 private void buildAndInitiateSearchTask(String searchType) { Projection mProjection = mMap.getProjection(); LatLng mProjectionCenter = mProjection.getVisibleRegion().latLngBounds .getCenter(); searchURL.append("https://maps.googleapis.com/maps/api/place/nearbysearch/"); searchURL.append("json?"); searchURL.append("location=" + mProjectionCenter.latitude + "," + mProjectionCenter.longitude); searchURL.append("&radius=" + calculateProjectionRadiusInMeters(mProjection)); searchURL.append("&key=YOUR_KEY_HERE"); new PlaceSearchAPITask().execute(searchURL.toString()); } private double calculateProjectionRadiusInMeters(Projection projection) { LatLng farLeft = projection.getVisibleRegion().farLeft; LatLng nearRight = projection.getVisibleRegion().nearRight; Location farLeftLocation = new Location("Point A"); farLeftLocation.setLatitude(farLeft.latitude); farLeftLocation.setLongitude(farLeft.longitude); Location nearRightLocation = new Location("Point B"); nearRightLocation.setLatitude(nearRight.latitude); nearRightLocation.setLongitude(nearRight.longitude); return farLeftLocation.distanceTo(nearRightLocation) / 2 ; } 

3)发送您的请求并将结果显示为AsyncTask

 private class PlaceSearchAPITask extends AsyncTask { @Override protected String doInBackground(String... placesURL) { StringBuilder placesBuilder = new StringBuilder(); for (String placeSearchURL : placesURL) { HttpClient placesClient = createHttpClient(); try { HttpGet placesGet = new HttpGet(placeSearchURL); HttpResponse placesResponse = placesClient .execute(placesGet); StatusLine placeSearchStatus = placesResponse .getStatusLine(); if (placeSearchStatus.getStatusCode() == 200) { HttpEntity placesEntity = placesResponse .getEntity(); InputStream placesContent = placesEntity .getContent(); InputStreamReader placesInput = new InputStreamReader( placesContent); BufferedReader placesReader = new BufferedReader( placesInput); String lineIn; while ((lineIn = placesReader.readLine()) != null) { placesBuilder.append(lineIn); } } } catch (Exception e) { e.printStackTrace(); } } return placesBuilder.toString(); } @Override protected void onPostExecute(String result) { try { JSONObject resultObject = new JSONObject(result); // This is my custom object to hold the pieces of the JSONResult that I want. You would need something else for your particular problem. mapData = new MapDataSource(resultObject.optJSONArray("results")); } catch (JSONException e) { e.printStackTrace(); } if (mapData != null) { // TODO - This is where you would add your markers and whatnot. } } } 
Interesting Posts