2012-04-21 114 views
1

我有一個複雜的谷歌地圖問題。我使用的是android和我繪製了2個具體點之間的路徑,但問題是路徑不準確,因爲它在可逆的街道上繪製,所以路徑現在是不對的任何人都可以有任何想法如何忽略錯誤的街道?谷歌地圖上的精確路徑

+0

我嘗試這樣的代碼,但我得到一個異常04-22 14:36:31.349:E/AndroidRuntime(335):致命異常:主 04-22 14:36:31.349:電子/ AndroidRuntime(335):java.lang.RuntimeException:無法實例化活動ComponentInfo {ee.xx.ss/ee.xx.ss.RoadProvider}:java.lang.ClassCastException:ee.xx.ss.RoadProvider 04-22 14:36:31.349:E/AndroidRuntime(335):\t at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)。我認爲它在xml解析器中。 我還有另外一個問題,我需要有一個類RoadProvider的佈局嗎? – 2012-04-22 12:44:27

+0

我試過了Pavan給我的代碼..在這個類中public static String getUrl(double fromLat,double fromLon,double toLat, double toLon)..我需要傳遞給它的值? – 2012-04-22 12:51:42

+0

@Agarwal是啊,你可以分享它,因爲我有其他項目的問題。 – 2012-04-22 12:53:07

回答

1

這是工作示例link。一探究竟。它有助於在地圖上創建路線覆蓋圖。這裏是complete source code

編輯: 爲亞歷山德羅以下提到:「這種方法,使用KML似乎沒有提供任何更多,這個問題已經在最後的日子#2討論Google Maps output=kml broken?

+0

使用'kml'的這種方法似乎不再可用,這個問題已經在最近幾天的Stackoverflow上討論過了。 'http:// stackoverflow.com/questions/11680872/google-maps-output-kml-broken' – AlexAndro 2012-08-06 08:44:19

+0

@AlexAndro謝謝您告訴我們。我會用你的帖子編輯答案。 – Pavan 2012-08-06 19:35:17

0

使用下面的類代碼::

import java.io.IOException; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.List; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.SAXException; 

import android.graphics.Color; 
import android.graphics.drawable.Drawable; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.OverlayItem; 



public class RoutePath extends MapActivity { 
    /** Called when the activity is first created. */ 

    MapView mapView; 
    private RoutePath _activity; 

    GeoPoint srcGeoPoint,destGeoPoint; 
    private static List<Overlay> mOverlays; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     _activity = this; 

     MapView mapView = (MapView) findViewById(R.id.mapview); 


     double src_lat = SRCLAT; 
     double src_long = SRCLNG; 
     double dest_lat = DESTLAT; 
     double dest_long = DESTLNG; 
     srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),(int) (src_long * 1E6)); 
     destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),(int) (dest_long * 1E6)); 

     List<Overlay> mapOverlays = mapView.getOverlays(); 
     Drawable srcdrawable = this.getResources().getDrawable(R.drawable.pin_green); 
     CustomItemizedOverlay srcitemizedOverlay = new CustomItemizedOverlay(srcdrawable, this); 
     OverlayItem srcoverlayitem = new OverlayItem(srcGeoPoint, "Hello!", "This is your Location."); 

     Drawable destdrawable = this.getResources().getDrawable(R.drawable.pin_red); 
     CustomItemizedOverlay destitemizedOverlay = new CustomItemizedOverlay(destdrawable, this); 
     OverlayItem destoverlayitem = new OverlayItem(destGeoPoint, "Hello!", "This is dest Location."); 

     srcitemizedOverlay.addOverlay(srcoverlayitem); 
     destitemizedOverlay.addOverlay(destoverlayitem); 

     mapOverlays.add(srcitemizedOverlay); 
     mapOverlays.add(destitemizedOverlay); 

     connectAsyncTask _connectAsyncTask = new connectAsyncTask(); 
     _connectAsyncTask.execute();   
     mapView.setBuiltInZoomControls(true); 
     mapView.displayZoomControls(true); 
     mOverlays = mapView.getOverlays(); 
     mapView.getController().animateTo(srcGeoPoint); 
     mapView.getController().setZoom(12); 
    } 


    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    private class connectAsyncTask extends AsyncTask<Void, Void, Void>{ 
     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 
      SHOW YOU PROGRESS BAR HERE 
     } 
     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 
      fetchData(); 
      return null; 
     } 
     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result);    
      if(doc!=null){ 
       Overlay ol = new MyOverLay(_activity,srcGeoPoint,srcGeoPoint,1); 
       mOverlays.add(ol); 
       NodeList _nodelist = doc.getElementsByTagName("status"); 
       Node node1 = _nodelist.item(0); 
       String _status1 = node1.getChildNodes().item(0).getNodeValue(); 
       if(_status1.equalsIgnoreCase("OK")){ 
        NodeList _nodelist_path = doc.getElementsByTagName("overview_polyline"); 
        Node node_path = _nodelist_path.item(0); 
        Element _status_path = (Element)node_path; 
        NodeList _nodelist_destination_path = _status_path.getElementsByTagName("points"); 
        Node _nodelist_dest = _nodelist_destination_path.item(0); 
        String _path = _nodelist_dest.getChildNodes().item(0).getNodeValue(); 
        List<GeoPoint> _geopoints = decodePoly(_path); 
        GeoPoint gp1; 
        GeoPoint gp2; 
        gp2 = _geopoints.get(0); 
        for(int i=1;i<_geopoints.size();i++) // the last one would be crash 
        { 

         gp1 = gp2; 
         gp2 = _geopoints.get(i); 
         Overlay ol1 = new MyOverLay(gp1,gp2,2,Color.BLUE); 
         mOverlays.add(ol1); 
        } 
        Overlay ol2 = new MyOverLay(_activity,destGeoPoint,destGeoPoint,3); 
        mOverlays.add(ol2); 

        DISMISS PROGRESS BAR HERE 

       }else{ 
        showAlert AS "Unable to find the route" 
       } 

       Overlay ol2 = new MyOverLay(_activity,destGeoPoint,destGeoPoint,3); 
       mOverlays.add(ol2); 
       DISMISS PROGRESS BAR HERE 
      }else{ 
       showAlert AS "Unable to find the route" 
      } 

     } 

    } 
    Document doc = null; 
    private void fetchData() 
    { 
     StringBuilder urlString = new StringBuilder(); 
     urlString.append("http://maps.google.com/maps/api/directions/xml?origin="); 
     urlString.append(Double.toString((double)srcGeoPoint.getLatitudeE6()/1.0E6)); 
     urlString.append(","); 
     urlString.append(Double.toString((double)srcGeoPoint.getLongitudeE6()/1.0E6)); 
     urlString.append("&destination=");//to 
     urlString.append(Double.toString((double)destGeoPoint.getLatitudeE6()/1.0E6)); 
     urlString.append(","); 
     urlString.append(Double.toString((double)destGeoPoint.getLongitudeE6()/1.0E6)); 
     urlString.append("&sensor=true&mode=driving"); 
     if(Constants.LOG)Log.d("xxx","URL="+urlString.toString());   
     HttpURLConnection urlConnection= null; 
     URL url = null; 
     try 
     { 
      url = new URL(urlString.toString()); 
      urlConnection=(HttpURLConnection)url.openConnection(); 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.setDoOutput(true); 
      urlConnection.setDoInput(true); 
      urlConnection.connect(); 
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      doc = (Document) db.parse(urlConnection.getInputStream());//Util.XMLfromString(response); 
     }catch (MalformedURLException e){ 
      e.printStackTrace(); 
     }catch (IOException e){ 
      e.printStackTrace(); 
     }catch (ParserConfigurationException e){ 
      e.printStackTrace(); 
     } 
     catch (SAXException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    private List<GeoPoint> decodePoly(String encoded) { 

     List<GeoPoint> poly = new ArrayList<GeoPoint>(); 
     int index = 0, len = encoded.length(); 
     int lat = 0, lng = 0; 

     while (index < len) { 
      int b, shift = 0, result = 0; 
      do { 
       b = encoded.charAt(index++) - 63; 
       result |= (b & 0x1f) << shift; 
       shift += 5; 
      } while (b >= 0x20); 
      int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
      lat += dlat; 

      shift = 0; 
      result = 0; 
      do { 
       b = encoded.charAt(index++) - 63; 
       result |= (b & 0x1f) << shift; 
       shift += 5; 
      } while (b >= 0x20); 
      int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
      lng += dlng; 

      GeoPoint p = new GeoPoint((int) (((double) lat/1E5) * 1E6), 
        (int) (((double) lng/1E5) * 1E6)); 
      poly.add(p); 
     } 

     return poly; 
    } 
} 

CustomItemizedOverlay.java

import java.util.ArrayList; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 

import com.google.android.maps.ItemizedOverlay; 
import com.google.android.maps.OverlayItem; 

public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> { 

    private final ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>(); 

    private Context context; 

    public CustomItemizedOverlay(Drawable defaultMarker) { 
     super(boundCenterBottom(defaultMarker)); 
    } 

    public CustomItemizedOverlay(Drawable defaultMarker, Context context) { 
     this(defaultMarker); 
     this.context = context; 
    } 

    @Override 
    protected OverlayItem createItem(int i) { 
     return mapOverlays.get(i); 
    } 

    @Override 
    public int size() { 
     return mapOverlays.size(); 
    } 

    public void addOverlay(OverlayItem overlay) { 
     mapOverlays.add(overlay); 
     this.populate(); 
    } 

} 

MyOverLay.java

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Point; 
import android.graphics.RectF; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.Projection; 

public class MyOverLay extends Overlay 
{ 
    private GeoPoint gp1; 
    private GeoPoint gp2; 
    //private int mRadius=6; 
    private int mode=0; 
    private int defaultColor; 
    private String text=""; 
    private Bitmap img = null; 
    Context mContext; 

    public MyOverLay(Context context,GeoPoint gp1,GeoPoint gp2,int mode) // GeoPoint is a int. (6E) 
    { 
     this.gp1 = gp1; 
     this.gp2 = gp2; 
     this.mode = mode; 
     this.mContext = context; 
     defaultColor = 999; // no defaultColor 

    } 

    public MyOverLay(GeoPoint gp1,GeoPoint gp2,int mode, int defaultColor) 
    { 
     this.gp1 = gp1; 
     this.gp2 = gp2; 
     this.mode = mode; 
     this.defaultColor = defaultColor; 
    } 
    public void setText(String t) 
    { 
     this.text = t; 
    } 
    public void setBitmap(Bitmap bitmap) 
    { 
     this.img = bitmap; 
    } 
    public int getMode() 
    { 
     return mode; 
    } 

    @Override 
    public boolean draw 
    (Canvas canvas, MapView mapView, boolean shadow, long when) 
    { 
     Projection projection = mapView.getProjection(); 
     if (shadow == false) 
     { 
      Paint paint = new Paint(); 
      paint.setAntiAlias(true); 
      Point point = new Point(); 
      projection.toPixels(gp1, point); 
      // mode=1&#65306;start 
      if(mode==1) 
      { 
       if(defaultColor==999) 
        paint.setColor(Color.BLUE); 
       else 
        paint.setColor(defaultColor);     
       // start point 
      } 
      // mode=2&#65306;path 
      else if(mode==2) 
      { 
       if(defaultColor==999) 
        paint.setColor(Color.RED); 
       else 
        paint.setColor(defaultColor); 
       Point point2 = new Point(); 
       projection.toPixels(gp2, point2); 
       paint.setStrokeWidth(5); 
       paint.setAlpha(120); 
       canvas.drawLine(point.x, point.y, point2.x,point2.y, paint); 

      } 
      /* mode=3&#65306;end */ 
      else if(mode==3) 
      { 
       /* the last path */ 

       if(defaultColor==999) 
        paint.setColor(Color.GREEN); 
       else 
        paint.setColor(defaultColor); 
       Point point2 = new Point(); 
       projection.toPixels(gp2, point2); 
       paint.setStrokeWidth(5); 
       paint.setAlpha(120); 
       canvas.drawLine(point.x, point.y, point2.x,point2.y, paint); 
       /* end point */ 

      } 
     } 
     return super.draw(canvas, mapView, shadow, when); 
    } 

} 
+0

請不要使用註釋進行狀態更新。答案旨在回答問題。如果您需要擴展討論,請在聊天中進行。 – BoltClock 2012-04-22 15:01:03

+0

@ BoltClock的獨角獸肯定 – 2012-04-22 16:32:49