【问题标题】:Error: java.lang.IllegalStateException: no included points错误:java.lang.IllegalStateException:没有包含点
【发布时间】:2018-04-16 13:27:36
【问题描述】:

尝试在地图上搜索地点时出现此错误。我在搜索时尝试了其他解决方案,但没有运气。

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;
}

图片:

【问题讨论】:

标签: java android google-maps google-places-api directions


【解决方案1】:

您当前的代码只是使用列表中的最后一条路线,但更常见的是使用列表中的第一条路线,而不是备用路线之一。

为了获得解码后的折线列表,您只需要查看routes JSONArray 的第一个元素,正如您在this working example 中看到的那样。

因此,删除 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);
}

【讨论】:

  • 感谢您的回复。我尝试了您的建议,它解决了我没有收到错误但没有设置标记或将路线映射到该地址的问题,它只是停留在该位置
  • @LizG 确保您从服务器获得了您所期望的。登录response.body().toString() 看看里面有什么。
  • 它给了我一个错误:此 IP、站点或移动应用程序无权使用此 API 密钥..请求被拒绝..我已启用方向和地点 API。
  • @LizG 如果您直接在应用程序中进行调用,您将需要使用不安全的 API 密钥作为路线 Web api。这个 api 真的是要与服务器密钥一起使用。看看文档:developers.google.com/maps/documentation/directions/get-api-key
  • @LizG 更多信息请看这里:stackoverflow.com/questions/46178231/…
猜你喜欢
  • 2019-08-21
  • 1970-01-01
  • 1970-01-01
  • 2017-08-09
  • 1970-01-01
  • 2013-11-12
  • 2019-11-14
  • 2016-09-19
  • 1970-01-01
相关资源
最近更新 更多