Danniel Magno
Danniel Magno

Reputation: 304

How to call populate() from mapOverlay

I'm converting GeoPoints from a route to an overlay and inserting to the map, on the overlays list (using mapView.getOverlays()).

On the first execution, everything went fine, but after, the map doesn't display the route overlay. After researching, I've remembered I have to call populate() to update the map overlays, but I don't know how to do that outside the ItemizedOverlay class.

Someone could help me?

Thanks in advance, Danniel.

EDIT::

Method which gets the route and parse the polyline into GeoPoints

public void drawRoute(Location from, double toLat, double toLon) {
    String url = RoadProvider.getUrl(from.getLatitude(), from.getLongitude(), toLat, toLon);
    Log.i("rotaUrl", url);

    JSONGet routeHttpObj = new JSONGet(mContext, url) {
        @SuppressWarnings("unchecked")
        @Override
        protected void jsonHandler(JSONObject routeJsonObj) {
            Log.i("Rota", "Rota recebida...");

            try {
                // Qual o status da requisição da rota?
                String status = routeJsonObj.getString("status").toString();

                // O webservice conseguiu traçar uma rota?
                if (status.equalsIgnoreCase("OK")) {
                    Log.i("Rota", "Ok");

                    Log.i("overlay", mapOverlays.toString());
                    Log.i("overlay", mapOverlays.size()+"");

                    // Remove a rota, caso exista
                    if (mapOverlays.size() == 3) {
                        mapOverlays.remove(2);
                    }

                    // Reseta o mapa
                    itemizedOverlay.populateNow();
                    mapView.invalidate();

                    Log.i("overlay", mapOverlays.size()+"");

                    // Extração da polyline codificada
                    String polyline = routeJsonObj.getJSONArray("routes").getJSONObject(0).getJSONObject("overview_polyline").getString("points");

                    // Decodificação da polyline para um array de GeoPoints
                    ArrayList<GeoPoint> route = RoadProvider.decodePolyline(polyline);

                    // Instanciamento do objeto da rota
                    RoadOverlay routeOverlay = new RoadOverlay(route);

                    // Pega a lista de overlays e adiciona a rota ao mapview
                    mapOverlays.add(routeOverlay);

                    // Reseta o mapa
                    itemizedOverlay.populateNow();
                    mapView.invalidate();

                    Log.i("overlay", mapOverlays.size()+"");
                    Log.i("overlay", mapOverlays.toString());

                    // Pega os passos da rota
                    /*JSONArray routeSteps = routeJsonObj.getJSONArray("routes").getJSONObject(0).getJSONObject("legs").getJSONArray("steps");

                    // Percorre os passos da rota e insere nos vetores, para guiar o motorista
                    for (int i=0; i<routeSteps.length(); i++) {
                        // O objeto do passo no indice i
                        JSONObject stepObj = routeSteps.getJSONObject(i);

                        // Cria o array dos pontos
                        GeoPoint[] arrPoints = new GeoPoint[2];

                        // Insere a latitude e longitude no array
                        //arrPoints[0] = stepObj.getJSONObject(name)

                        // Insere o array na lista
                        routePoints.add(arrPoints);
                        // routeInstructions
                    }*/

                    // Copia os pontos para referencia futura
                    routePoints = (ArrayList<GeoPoint>) route.clone();
                } else {
                    // Qual o erro?
                    if (status.equalsIgnoreCase("NOT_FOUND")) {

                    } else if (status.equalsIgnoreCase("ZERO_RESULTS")) {

                    } else if (status.equalsIgnoreCase("MAX_WAYPOINTS_EXCEEDED")) {

                    } else if (status.equalsIgnoreCase("INVALID_REQUEST")) {

                    } else if (status.equalsIgnoreCase("OVER_QUERY_LIMIT") || status.equalsIgnoreCase("REQUEST_DENIED")) {

                    } else {

                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    // Desabilita a exibição do loading
    routeHttpObj.setDisplayDialog(false);

    // Executa a requisição
    routeHttpObj.execute();
}

Class which draws the route overlay

public class RoadOverlay extends Overlay {
private ArrayList<GeoPoint> pointList;

public RoadOverlay(ArrayList<GeoPoint> pointList) {
    this.pointList = pointList;
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    Point current = new Point();
    Path path = new Path();
    Projection projection = mapView.getProjection();
    Iterator<GeoPoint> iterator = pointList.iterator();

    if (iterator.hasNext()) {
        projection.toPixels(iterator.next(), current);
        path.moveTo((float) current.x, (float) current.y);
    } else {
        return;
    }

    while(iterator.hasNext()) {
        projection.toPixels(iterator.next(), current);
        path.lineTo((float) current.x, (float) current.y);
    }

    Paint pathPaint = new Paint();
    pathPaint.setAntiAlias(true);
    pathPaint.setStrokeWidth(8.0f);
    pathPaint.setColor(Color.argb(64, 0, 0, 255));
    pathPaint.setStyle(Style.STROKE);
    canvas.drawPath(path, pathPaint);
}
}

Upvotes: 0

Views: 230

Answers (1)

Akram
Akram

Reputation: 7526

Create a method inside your itemized overlay class and put the populate(); inside that method and call that method using instance of itemized class.

Inside the Itemized class

void populateNow()
{
    populate();
}

call it as follows

mItemized.populateNow();

Upvotes: 1

Related Questions