如何根据当前位置设置正确的纬度和经度

5

我的目标是使用Google Places API进行自动完成预测,现在我想制定一种算法,该算法将获取当前位置的纬度和经度,并预测仅在100-200公里半径内的地点。

因此,目前我已获取了用户当前位置的纬度和经度,如何设置100-200公里的半径呢?

 private void getCurrentLocation() {
    mLastLocation = LocationServices.FusedLocationApi
            .getLastLocation(mGoogleApiClient);
    if (mLastLocation != null) {
        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();
        mLatLonBounds = new LatLngBounds(new LatLng(latitude,longitude),
                                  new LatLng(latitude,longitude));
        Log.d("myTag","lat = "+mLatLonBounds.northeast.latitude+" ,lon = "+mLatLonBounds.northeast.longitude);
        //Log.d("myTag","lat = "+mLatLonBounds.southwest.latitude+" ,lon = "+mLatLonBounds.southwest.longitude);

    }else {
        //some code
    }
}

以下是我如何设置自动预测的边界:

  @Nullable
private ArrayList<AutoCompletePlace> getAutocomplete(CharSequence constraint) {
    if (mGoogleApiClient.isConnected()) {
        Log.i(Constants.AUTO_COMPLETE_TAG, "Starting autocomplete query for: " + constraint);

        // Submit the query to the autocomplete API and retrieve a PendingResult that will
        // contain the results when the query completes.
        PendingResult<AutocompletePredictionBuffer> results = Places.GeoDataApi
                        .getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
                                **mBounds**, mPlaceFilter);

        // This method should have been called off the main UI thread. Block and wait for at most 60s
        // for a result from the API.
        AutocompletePredictionBuffer autocompletePredictions = results.await(60, TimeUnit.SECONDS);

        // Confirm that the query completed successfully, otherwise return null
        final Status status = autocompletePredictions.getStatus();
        if (!status.isSuccess()) {
            Toast.makeText(getContext(), "Error contacting API: " + status.toString(),
                    Toast.LENGTH_SHORT).show();
            Log.e(Constants.AUTO_COMPLETE_TAG, "Error getting autocomplete prediction API call: " + status.toString());
            autocompletePredictions.release();
            return null;
        }

        Log.i(Constants.AUTO_COMPLETE_TAG, "Query completed. Received " + autocompletePredictions.getCount()
                + " predictions.");

        // Copy the results into our own data structure, because we can't hold onto the buffer.
        // AutocompletePrediction objects encapsulate the API response (place ID and description).

        Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
        ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
        while (iterator.hasNext()) {
            AutocompletePrediction prediction = iterator.next();
            // Get the details of this prediction and copy it into a new PlaceAutocomplete object.
            resultList.add(new AutoCompletePlace(prediction.getPlaceId(),
                    prediction.getDescription()));
        }

        // Release the buffer now that all data has been copied.
        autocompletePredictions.release();

        return resultList;
    }
    Log.e(Constants.AUTO_COMPLETE_TAG, "Google API client is not connected for autocomplete query.");
    return null;

我的当前位置示例为48.6180288,22.2984587。

更新:在Francois Wouts给出答案之前,我在stackoverflow上找到了另一种解决方案,您也可以使用它。

 public static final LatLngBounds setBounds(Location location, int mDistanceInMeters ){
    double latRadian = Math.toRadians(location.getLatitude());
    double degLatKm = 110.574235;
    double degLongKm = 110.572833 * Math.cos(latRadian);
    double deltaLat = mDistanceInMeters / 1000.0 / degLatKm;
    double deltaLong = mDistanceInMeters / 1000.0 / degLongKm;

    double minLat = location.getLatitude() - deltaLat;
    double minLong = location.getLongitude() - deltaLong;
    double maxLat = location.getLatitude() + deltaLat;
    double maxLong = location.getLongitude() + deltaLong;

    Log.d("Location", "Min: " + Double.toString(minLat) + "," + Double.toString(minLong));
    Log.d("Location","Max: "+Double.toString(maxLat)+","+Double.toString(maxLong));

    // Set up the adapter that will retrieve suggestions from the Places Geo Data API that cover
    // the entire world.

    return new LatLngBounds(new LatLng(minLat,minLong),new LatLng(maxLat,maxLong));

你查看过这个了吗? - makata
1个回答

7
根据维基百科,您可能希望允许每个方向大约1度的范围覆盖100-200公里左右的用户位置。覆盖的确切区域将取决于用户所在的位置,但对于大多数用例来说,这应该是一个足够好的近似值。
例如,请尝试以下操作:
double radiusDegrees = 1.0;
LatLng center = /* the user's location */;
LatLng northEast = new LatLng(center.latitude + radiusDegrees, center.longitude + radiusDegrees);
LatLng southWest = new LatLng(center.latitude - radiusDegrees, center.longitude - radiusDegrees);
LatLngBounds bounds = LatLngBounds.builder()
        .include(northEast)
        .include(southWest)
        .build();

我相信这应该可以正确地跨越本初子午线。让我知道你的进展如何!


谢谢你的回答,我会查看一下。center 是一个 Location 对象吗? - Dennis Zinkovski
这是一个LatLng,抱歉在初始答案中没有表述清楚。 - Francois Wouts

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接