放置api集位置基于当前位置的偏向

时间:2019-08-16 04:41:04

标签: android google-places-api

我正在使用新的AutocompleteSupportFragment,因此用户可以搜索地方并选择自己感兴趣的地方。我希望结果在用户周围。我了解我可以设置LocationBias,但需要RectangularBounds

我不知道矩形的边界,因为我只有当前位置LatLng。如何实现所需的行为?

谢谢

1 个答案:

答案 0 :(得分:0)

您可以根据用户当前位置设置latlong范围。下面是相同的示例。

private void 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(TAG,"Min: "+Double.toString(minLat)+","+Double.toString(minLong));
    Log.d(TAG,"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.
    mAdapter = new PlaceAutocompleteAdapter(this, android.R.layout.simple_list_item_1,
            mGoogleApiClient, new LatLngBounds(new LatLng(minLat, minLong), new LatLng(maxLat, maxLong)), null);
    mAutocompleteView.setAdapter(mAdapter);
}

有关更多参考,您可以点击以下链接

Set Bounds for Places Api