我在尝试搜索地图上的地点时收到此错误。我在搜索时尝试了其他分辨率,但没有运气。
java.lang.IllegalStateException:没有包含点
在这一行:LatLngBounds.Builder builder = new LatLngBounds.Builder();
我正在使用的代码:
try {
JSONObject jsonObject = new JSONObject(response.body().toString());
JSONArray jsonArray = jsonObject.getJSONArray("routes");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject route = jsonArray.getJSONObject(i);
JSONObject poly = route.getJSONObject("overview_polyline");
String polyline = poly.getString("points");
polyLineList = decodePoly(polyline);
}
// Adjusting Bounds
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng latLng:polyLineList) {
builder = builder.include(latLng);
}
LatLngBounds bounds = builder.build();
CameraUpdate mCameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 2);
mMap.animateCamera(mCameraUpdate);
private List decodePoly(String encoded) {
List poly = new ArrayList();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLng p = new LatLng((((double) lat / 1E5)), (((double) lng / 1E5)));
poly.add(p);
}
return poly;
}
Image:
您当前的代码仅使用列表中的最后一个路由,但更常见的是使用列表中的第一个路由,而不是使用备用路由之一。
为了获得解码后的折线列表,您只需查看该折线的第一个元素routes
JSONArray,如您所见这个工作示例 https://stackoverflow.com/a/32940175/4409409.
因此,删除 for 循环并得到overview_polyline
从第一个元素开始:
JSONArray routeArray = jsonObject.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
polyLineList = decodePoly(encodedString);
这应该可以解决最常见的情况,即您成功地从请求中获取数据。
为了安全起见,无论何时您在处理问题时LatLngBounds.Builder
,您应该确保您有一个非空数据集。
这将确保您永远不会收到 IllegalStateException:
if (!polyLineList.isEmpty()) {
// Adjusting Bounds
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng latLng:polyLineList) {
builder = builder.include(latLng);
}
LatLngBounds bounds = builder.build();
CameraUpdate mCameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, 2);
mMap.animateCamera(mCameraUpdate);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)