0
我正試圖建立一個GPS應用程序,它可以顯示2個地方之間的方向。爲了解碼多義線,我使用了以下代碼:Decoding Polylines from Google Maps Direction API with Java。我想比這更準確地顯示我的路線,我可以通過kml來實現,但只有在文件大小達到極限之後才能用於非常小的距離。下面是截圖波士頓,西雅圖要在地圖上線不遵守道路,但相交道路
谷歌地圖路線沒有正確顯示
下面是我使用方向API
public void drawRoute(String source, String destination)
{
String strURL = "http://maps.google.com/maps/api/directions/xml?origin=" + source +
"&destination=" + destination + "&sensor=false&mode=driving";
String url = strURL.replace(" ", "%20");
HttpGet get = new HttpGet(url);
String strResult = "";
try {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse httpResponse = null;
httpResponse = httpClient.execute(get);
if (httpResponse.getStatusLine().getStatusCode() == 200){
strResult = EntityUtils.toString(httpResponse.getEntity());
}
}
catch (Exception e){ }
if (-1 == strResult.indexOf("<status>OK</status>")){
this.finish();
return;
}
int pos = strResult.indexOf("<overview_polyline>");
pos = strResult.indexOf("<points>", pos + 1);
int pos2 = strResult.indexOf("</points>", pos);
strResult = strResult.substring(pos + 8, pos2);
List<GeoPoint> points = decodePoly(strResult);
RouteOverlay mOverlay = new RouteOverlay(points);
overlayList.add(mOverlay);
if (points.size() >= 2){
controller.animateTo(points.get(0));
}
map.invalidate();
}
這裏是我的RouteOverlay類:
public class RouteOverlay extends Overlay{
private List<GeoPoint> points;
private Paint paint;
public RouteOverlay(List<GeoPoint> points) {
this.points = points;
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setAlpha(150);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(4);
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
if (!shadow)
{
Projection projection = mapView.getProjection();
if (points != null && points.size() >= 2)
{
Point start = new Point();
projection.toPixels(points.get(0), start);
for (int i = 1; i < points.size(); i++)
{
Point end = new Point();
projection.toPixels(points.get(i), end);
canvas.drawLine(start.x, start.y, end.x, end.y, paint);
start = end;
}
}
}
}
}
您的問題是否得到解決? – 2012-04-29 03:24:36
@Agarwal不,它還沒有解決。 – Yogesh 2012-04-30 04:17:10