2012-04-25 53 views
0

我正試圖建立一個GPS應用程序,它可以顯示2個地方之間的方向。爲了解碼多義線,我使用了以下代碼:Decoding Polylines from Google Maps Direction API with Java。我想比這更準確地顯示我的路線,我可以通過kml來實現,但只有在文件大小達到極限之後才能用於非常小的距離。下面是截圖波士頓,西雅圖要在地圖上線不遵守道路,但相交道路
enter image description here谷歌地圖路線沒有正確顯示

下面是我使用方向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; 
      } 
     } 
    } 
} 
} 
+0

您的問題是否得到解決? – 2012-04-29 03:24:36

+0

@Agarwal不,它還沒有解決。 – Yogesh 2012-04-30 04:17:10

回答

1

請參考此link在您的應用程序中繪製驅動路徑。您只需在項目中添加鏈接中的四個類,並在需要顯示路徑時調用以下行。

SharedData data = SharedData.getInstance(); 
data.setAPIKEY("0RUTLH7cqd6yrZ0FdS0NfQMO3lioiCbnH-BpNQQ");//set your map key here 
data.setSrc_lat(17);//set your src lat 
data.setSrc_lng(78);//set your src lng 
data.setDest_lat(18);//set your dest lat 
data.setDest_lng(77);//set your dest lng 
startActivity(new Intent(YourActivity.this,RoutePath.class));//add RoutePath in your manifeast file 

//還允許添加到您的manifeast文件

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> 

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

<uses-permission android:name="android.permission.INTERNET"></uses-permission>