我指的是这个帖子
我遇到了一个异常,所以我尝试使用 AsyncTask ,就像这篇文章中的最后一个答案一样 。现在应用程序正在运行,没有任何异常,但它没有绘制路径。我不知道是什么问题。
这是代码:http ://www.mediafire.com/? lxm7gplzsfdtsx5 我只是用下面的 MainActivity.java 替换 itineraire.java
public 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;
}
}