7

I am getting this error when trying to search a place on map. I tried other resolutions when searching, but no luck.

java.lang.IllegalStateException: no included points

on this line: LatLngBounds.Builder builder = new LatLngBounds.Builder();

The code I am using:

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:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
LizG
  • 2,246
  • 1
  • 23
  • 38
  • I think this might be a duplicate of [Android GoogleMaps V2 MarkerDemo IllegalStateException no included points](https://stackoverflow.com/questions/14878077/android-googlemaps-v2-markerdemo-illegalstateexception-no-included-points) – Anna Harrison Nov 03 '17 at 18:26
  • I looked at that one and still not able to fix. How can I implement this into the code I provided? This is a tutorial I am doing. – LizG Nov 03 '17 at 18:27

1 Answers1

9

Your current code is just using the last route in the list, but it is more common to use the first route in the list, rather than one of the alternate routes.

In order to get the decoded polyline list, you just need to look in the first element of the routes JSONArray, as you can see in this working example.

So, remove the for loop and get overview_polyline from the first element:

JSONArray routeArray = jsonObject.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
polyLineList = decodePoly(encodedString);

That should take care of the most common case, where you successfully get data from the request.

Just to be on the safe side, anytime you are dealing with a LatLngBounds.Builder, you should ensure that you have a non-empty data set.
This will ensure that you never get the 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);
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Thanks for the reply. I tried what you suggested and it fixes the problem that I don't get an error but it doesn't set the marker or map the route to that address, it just stays at the location – LizG Nov 03 '17 at 19:20
  • 1
    @LizG Make sure that you're getting what you expect from the server. Log `response.body().toString()` and see what is in there. – Daniel Nugent Nov 03 '17 at 19:21
  • its giving me an error: This IP, site or mobile application is not authorized to use this API key .. REQUEST DENIED .. I have the directions and places apis enabled. – LizG Nov 03 '17 at 19:26
  • I uploaded an image of the apis I have enabled – LizG Nov 03 '17 at 19:31
  • 1
    @LizG You will need to use an unsecured API key for the directions web api, if you're making calls directly in the app. This api is really meant to be used with a Server key. Take a look at the documentation: https://developers.google.com/maps/documentation/directions/get-api-key – Daniel Nugent Nov 03 '17 at 19:43
  • 1
    @LizG See here for more info: https://stackoverflow.com/questions/46178231/android-google-maps-direction-api-api-key-restriction-not-working – Daniel Nugent Nov 03 '17 at 19:49
  • The problem was Key restriction. – LizG Nov 03 '17 at 20:04