【问题标题】:Google Places API: How to get photos and place id from latitude and longitude?Google Places API:如何从纬度和经度获取照片和地点 ID?
【发布时间】:2014-07-17 10:24:26
【问题描述】:

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

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

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

【问题讨论】:

  • 当然我们可以帮助你,但在此之前你必须展示你的研究和研发!

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


【解决方案1】:

根据Google Place Search 文档,您需要提供的三件事是密钥、位置和半径。我删掉了一堆不必要的代码,但我做了类似的事情。

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

【讨论】:

    猜你喜欢
    • 2017-10-04
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 2022-07-15
    • 2014-01-21
    • 1970-01-01
    相关资源
    最近更新 更多