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

时间:2014-07-17 10:24:27

标签: java android google-places-api google-places

我想获得当前位置的Google Place photos API所需的Details和place_id。

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

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

1 个答案:

答案 0 :(得分:3)

根据Google Place Search文档,您需要提供的三件事是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)构建您的搜索网址。你可以通过附加它们来添加像关键字这样的额外参数,但在这种特殊情况下听起来并不像你想要的那样。

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<String, Void, String> {

    @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.
        } 
    }
}