0

我正在将 GeoPoints 从路线转换为叠加层,并在叠加层列表上插入地图(使用 mapView.getOverlays())。

在第一次执行时,一切正常,但之后,地图不显示路线覆盖。经过研究,我记得我必须调用 populate() 来更新地图叠加层,但我不知道如何在 ItemizedOverlay 类之外执行此操作。

有人可以帮助我吗?

在此先感谢,丹尼尔。

编辑::

获取路线并将折线解析为 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();
}

绘制路线覆盖的类

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

1 回答 1

1

在您的内部创建一个方法itemized overlay class并将该方法放入populate();该方法并使用逐项类的实例调用该方法。

在 Itemized 类中

void populateNow()
{
    populate();
}

如下调用

mItemized.populateNow();
于 2012-05-25T16:12:19.550 回答