我正在引用此post
我得到一个异常,所以我尝试使用AsyncTask,就像这个post中的最后一个答案一样 。现在应用程序正在运行任何异常,但它不会绘制路径。我不知道是什么问题。
以下是代码:http://www.mediafire.com/?lxm7gplzsfdtsx5 我只需将itineraire.java替换为
下面的MainActivity.javapublic class MainActivity extends MapActivity {
MapView mapView = null;
MapController mapController;
double long1 = 50.123*1E6;
double lati1 = 13.123*1E6;
double long2 = 50.221*1E6;
double lati2 = 13.221*1E6;
GeoPoint start, dest;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_itineraire);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
DirectionsTask getDirectionsTask = new DirectionsTask();
getDirectionsTask.execute(new GeoPoint((int)(long1),(int)(lati1)), new GeoPoint((int)(long2),(int)(lati2)));
}
private class DirectionsTask extends AsyncTask<GeoPoint, Void, Route> {
protected Route doInBackground(GeoPoint...geoPoints) {
start = geoPoints[0];
dest = geoPoints[1];
Parser parser;
String jsonURL = "http://maps.google.com/maps/api/directions/json?";
final StringBuffer sBuf = new StringBuffer(jsonURL);
sBuf.append("origin=");
sBuf.append(start.getLatitudeE6()/1E6);
sBuf.append(',');
sBuf.append(start.getLongitudeE6()/1E6);
sBuf.append("&destination=");
sBuf.append(dest.getLatitudeE6()/1E6);
sBuf.append(',');
sBuf.append(dest.getLongitudeE6()/1E6);
sBuf.append("&sensor=true&mode=driving");
Log.v("I came in URL", sBuf.toString());
parser = new GoogleParser(sBuf.toString());
Route r = parser.parse();
return r;
}
protected void onPostExecute(Route route) {
RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
mapView.getOverlays().add(routeOverlay);
mapView.invalidate();
mapController = mapView.getController();
mapController.setZoom(14);
mapView.getMapCenter();
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
答案 0 :(得分:0)
您的路线是在地图上绘制的,但是如果您移动地图就会在屏幕上显示您的路线,我建议您使用新的Maps API,它可以更好,更轻松地在地图上绘制路线
使用当前的实现,您可以使用 mapView.getController().zoomToSpan((int)lati1,(int)long1);
将地图置于中心位置