错误:java.lang.IllegalStateException:没有包含的点

7

当我尝试在地图上搜索地点时,出现了以下错误。我尝试了其他的搜索分辨率,但是没有成功。

java.lang.IllegalStateException: no included points

问题出现在这一行: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;
}

图片:

在此输入图片描述


这是一张图片,点击图片可以查看更大的图片。

我认为这可能是Android GoogleMaps V2 MarkerDemo IllegalStateException no included points的重复问题。 - Anna Harrison
我看了那个,但仍然无法修复。我怎样将其实现到我提供的代码中?这是我正在做的教程。 - LizG
1个回答

9
你当前的代码只使用列表中的最后一个路线,但更常见的做法是使用列表中的第一个路线,而不是其中的备选路线。
为了获取解码后的折线列表,你只需查看routes JSONArray的第一个元素,正如这个可行的示例所示。
因此,删除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);
}

1
@LizG 确保从服务器获取到的内容符合您的期望。记录下 response.body().toString() 并查看其中包含了什么。 - Daniel Nugent
我上传了我启用的API的图像。 - LizG
1
@LizG 如果您直接在应用程序中进行调用,则需要使用未加密的API密钥来使用方向Web API。该API实际上是为服务器密钥而设计的。请查看文档:https://developers.google.com/maps/documentation/directions/get-api-key - Daniel Nugent
1
@LizG 这里有更多信息:https://dev59.com/t1YO5IYBdhLWcg3wN-6f - Daniel Nugent
问题是密钥限制。 - LizG
显示剩余2条评论

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