如何解决如何限制Places Autocomplete api返回仅距当前位置2公里以内的位置
是否可以将自动填充位置限制为仅返回距我当前位置2公里范围内的结果?在城市内?
我正在使用来自官方网站的代码,我尝试使用将原点设置为当前位置和范围,但没有得到理想的结果,通过尝试此操作,我得到的是国家/地区而不是城市,而不是2km我当前的位置,我该如何实现?
预先感谢
解决方法
将没有直接的方法来指定当前位置和半径,但是您可以通过简单的数据处理来实现。
public void openPlacePickerActivity() {
List<Place.Field> fields = Arrays.asList(Place.Field.ID,Place.Field.NAME,Place.Field.LAT_LNG);
//Specify your radius
double radius = 2.0;
// Get Rectangular Bounds from your current location
double[] boundsFromLatLng = getBoundsFromLatLng(radius,-33.880490f,151.184363f);
Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN,fields)
// Specify Rectangular Bounds to restrict the API result.
// Ref: https://developers.google.com/places/android-sdk/autocomplete#restrict_results_to_a_specific_region
.setLocationRestriction(RectangularBounds.newInstance(
new LatLng(boundsFromLatLng[0],boundsFromLatLng[1]),new LatLng(boundsFromLatLng[2],boundsFromLatLng[3])
))
.build(mActivity);
startActivityForResult(intent,101);
}
/**
* Please check below link for an understanding of the method
* Ref: https://stackoverflow.com/questions/238260/how-to-calculate-the-bounding-box-for-a-given-lat-lng-location#41298946
*/
public double[] getBoundsFromLatLng(double radius,double lat,double lng) {
double lat_change = radius / 111.2f;
double lon_change = Math.abs(Math.cos(lat * (Math.PI / 180)));
return new double[]{
lat - lat_change,lng - lon_change,lat + lat_change,lng + lon_change
};
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。