我正在将GeoPoints从路径转换为叠加层并插入到地图上的叠加列表中(使用mapView.getOverlays())。
在第一次执行时,一切都很顺利,但之后,地图不会显示路线覆盖。经过研究,我记得我必须调用populate()来更新地图叠加层,但我不知道如何在ItemizedOverlay类之外做到这一点。
有人可以帮助我吗?
提前致谢, 丹尼尔。
EDIT ::
获取路径并将折线解析为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);
}
}
答案 0 :(得分:1)
在itemized overlay class
中创建一个方法,并将populate();
放在该方法中,并使用逐项类的实例调用该方法。
在Itemized类中
void populateNow()
{
populate();
}
将其命名如下
mItemized.populateNow();