1

I am having an error when zooming in Google Maps with CameraUpdateFactory.newLatLngBounds. Before I did not have this error, but I had to put a MapView and I started to give the error in the line:

    mapa.moveCamera(CameraUpdateFactory.newLatLngBounds(bc.build(), 60));

JAVA

private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String,String>>> >{

    protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {

        JSONObject jObject;
        List<List<HashMap<String, String>>> routes = null;

        try {
            jObject = new JSONObject(jsonData[0]);
            DirectionsJSONParser parser = new DirectionsJSONParser();

            routes = parser.parse(jObject);

        } catch(Exception e) {
            e.printStackTrace();
        }
        return routes;
    }

    protected void onPostExecute(List<List<HashMap<String, String>>> result) {

        ArrayList<LatLng> points = null;
        PolylineOptions lineOptions = null;

        String distancia = "";
        String duracion = "";

        if(result.size() < 1) {
            Toast.makeText(context, "Error. No se encontraron puntos", Toast.LENGTH_SHORT).show();
            return;
        }

        for(int i = 0; i < result.size(); i++) {

            points = new ArrayList<LatLng>();
            lineOptions = new PolylineOptions();

            List<HashMap<String, String>> path = result.get(i);

            for(int j = 0; j < path.size(); j++) {

                HashMap<String,String> point = path.get(j);

                if(j == 0) {
                    distancia = (String)point.get("distance");
                    continue;

                } else if(j == 1) {
                    duracion = (String)point.get("duration");
                    continue;
                }

                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));
                LatLng position = new LatLng(lat, lng);

                points.add(position);
            }

            lineOptions.addAll(points);
            lineOptions.width(6);
            lineOptions.color(getResources().getColor(cargarColorRuta(perfil.getColor())));
        }

        String nuevaDuracion = duracion.replaceAll("hours", "h.").replace("mins", "min");

        pintarDistanciaDuracion(OPC_TXT_CON_DATOS, distancia, nuevaDuracion);

        mapa.addPolyline(lineOptions);
        zoomPuntos(lineOptions);
    }
}

 private void zoomPuntos(PolylineOptions lineOptions) {

    List<LatLng> points = lineOptions.getPoints(); // route is instance of PolylineOptions 

    LatLngBounds.Builder bc = new LatLngBounds.Builder();

    for (LatLng item : points) {
        bc.include(item);
    }

    mapa.moveCamera(CameraUpdateFactory.newLatLngBounds(bc.build(), 60)); // ERROR IN THIS LINE
 }

ERROR

02-25 12:57:17.121: E/AndroidRuntime(4527): java.lang.IllegalStateException: Map size should not be 0. Most likely, layout has not yet occured for the map view.
Charlie
  • 325
  • 1
  • 7
  • 22

0 Answers0