Android Google Map AutoComplete GeoDataAPI 给出范围特定的结果
Android Google Map AutoComplete GeoDataAPI giving bounds specific results
我正在 google 地图上实现搜索功能。为此,我使用 google 的 Places.GeoDataApi。
但我面临的问题是它是根据 LatLanBounds 值进行搜索。
目前我正在通过以下 api 中的悉尼特定边界,因此在搜索时它偏向于这些边界并首先检查 syndey 特定位置。
private static final LatLngBounds mBounds= new LatLngBounds(
new LatLng(-34.041458, 150.790100), new LatLng(-33.682247, 151.383362));
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi
.getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
mBounds, mPlaceFilter);
按照我的要求,上面API应该return先按区域附近的结果。例如,如果用户在印度并搜索某些东西,它应该 return 优先考虑与印度相关的建议,如果巴西用户尝试搜索某些东西 API 应该 return 与以下相关的建议巴西。我不知道如何根据用户当前国家或位置传递动态 LatLng 边界值。
非常感谢您的帮助。
这是一个月前的问题,但我希望我的回答能对 OP 或遇到类似问题的其他人有所帮助。
主要问题的答案非常简单明了:
每次获取有关用户位置的信息时创建新的 LatLngBounds (docs) 对象并将该对象作为 .getAutocompletePredictions 的参数传递:
LatLng sw = new LatLng(SOUTH_WEST_LAT, SOUTH_WEST_LNG);
LatLng ne = new LatLng(NORTH_EAST_LAT, NORTH_EAST_LNG);
LatLngBounds bounds = new LatLngBounds(sw, ne);
Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, constraint, bounds, null);
These two points, south-west and north-east are opposite corners of the square that makes an area around user's location. This is the bound.
关于你的第二个问题,在评论中。我想你有你的用户位置(例如来自 GPS),例如在巴西,你需要找到这个位置的边界。
为此,我将使用基本的 Google Places API 调用(您可以在浏览器中尝试):
https://maps.googleapis.com/maps/api/geocode/json?address=Brazil
这将 return 您 JSON 格式化了关于巴西的数据,其中包含字段 bounds:
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 42.1282269,
"lng" : -87.7108162
},
"southwest" : {
"lat" : 42.0886089,
"lng" : -87.7708629
}
},
...
}
如您所见,这些是巴西所在广场的东北和西南边界。您现在可以将它们放入 LatLngBounds 对象中。
我正在 google 地图上实现搜索功能。为此,我使用 google 的 Places.GeoDataApi。
但我面临的问题是它是根据 LatLanBounds 值进行搜索。 目前我正在通过以下 api 中的悉尼特定边界,因此在搜索时它偏向于这些边界并首先检查 syndey 特定位置。
private static final LatLngBounds mBounds= new LatLngBounds(
new LatLng(-34.041458, 150.790100), new LatLng(-33.682247, 151.383362));
PendingResult<AutocompletePredictionBuffer> results =
Places.GeoDataApi
.getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
mBounds, mPlaceFilter);
按照我的要求,上面API应该return先按区域附近的结果。例如,如果用户在印度并搜索某些东西,它应该 return 优先考虑与印度相关的建议,如果巴西用户尝试搜索某些东西 API 应该 return 与以下相关的建议巴西。我不知道如何根据用户当前国家或位置传递动态 LatLng 边界值。
非常感谢您的帮助。
这是一个月前的问题,但我希望我的回答能对 OP 或遇到类似问题的其他人有所帮助。
主要问题的答案非常简单明了: 每次获取有关用户位置的信息时创建新的 LatLngBounds (docs) 对象并将该对象作为 .getAutocompletePredictions 的参数传递:
LatLng sw = new LatLng(SOUTH_WEST_LAT, SOUTH_WEST_LNG);
LatLng ne = new LatLng(NORTH_EAST_LAT, NORTH_EAST_LNG);
LatLngBounds bounds = new LatLngBounds(sw, ne);
Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, constraint, bounds, null);
These two points, south-west and north-east are opposite corners of the square that makes an area around user's location. This is the bound.
关于你的第二个问题,在评论中。我想你有你的用户位置(例如来自 GPS),例如在巴西,你需要找到这个位置的边界。 为此,我将使用基本的 Google Places API 调用(您可以在浏览器中尝试):
https://maps.googleapis.com/maps/api/geocode/json?address=Brazil
这将 return 您 JSON 格式化了关于巴西的数据,其中包含字段 bounds:
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 42.1282269,
"lng" : -87.7108162
},
"southwest" : {
"lat" : 42.0886089,
"lng" : -87.7708629
}
},
...
}
如您所见,这些是巴西所在广场的东北和西南边界。您现在可以将它们放入 LatLngBounds 对象中。