借鉴谷歌地图的路线,机器人机器人、路线、地图

2023-09-07 10:01:48 作者:陌上曲一首,曲散终离别

嘿,我想这code可以连接到谷歌地图通过传递源和目标坐标,以这个URL,并获得 KML 文件。但是我不能够解析或可以连接到这个网址。

任何人能看到我要去哪里错了?

 公共类RoutePath扩展MapActivity {
    图形页面图形页面;
    的GeoPoint GP1,GP2;
    MapController MC;

    公共类MyOverLay扩展覆盖{
        私人诠释mRadius = 6;
        私人诠释模式= 0;
        私人诠释defaultColor;
        私人字符串文本=;
        私人位图IMG = NULL;

        公共MyOverLay(){
        }

        公共MyOverLay(的GeoPoint P1,P2的GeoPoint,INT模式){// GeoPoint对象是一个int类型。 (6E)
            GP1 = P1;
            GP2 = P2;
            this.mode =模式;
            defaultColor = 999; //没有defaultColor
        }

        公共MyOverLay(的GeoPoint P1,P2的GeoPoint,诠释方式,诠释defaultColor){
            GP1 = P1;
            GP2 = P2;
            this.mode =模式;
            this.defaultColor = defaultColor;
        }

        公共无效的setText(串T){
            this.text = T;
        }

        公共无效setBitmap(位图位图){
            this.img =位图;
        }

        公众诠释获取模式(){
            返回模式;
        }

        @覆盖
        公共布尔平局(画布油画,图形页面图形页面,布尔影子,时长){
            super.draw(帆布,图形页面,阴影);
            点screenPts =新的点();
            MapView.getProjection()在toPixels(GP1,screenPts)。
            BMP位= BitmapFactory.de codeResource(
            getResources(),R.drawable.pushpin);
            canvas.drawBitmap(BMP,screenPts.x,screenPts.y-50,空);
            返回true;
        }

        公共布尔的onTouchEvent(MotionEvent事件,图形页面图形页面){
            //当用户抬起手指
            如果(event.getAction()== 1){
                Toast.makeText(getBaseContext(),距离,Toast.LENGTH_SHORT).show();
                的GeoPoint GP2 = MapView.getProjection()在。fromPixels(
                (int)的event.getX(),
                (int)的event.getY());

                Toast.makeText(getBaseContext(),gp2.getLatitudeE6()/ 1E6 +,+ gp2.getLongitudeE6()/ 1E6,Toast.LENGTH_SHORT).show();

                双src_lat = 19.0552; //测试源
                双src_long = 72.8308;
                //双dest_lat = 19.0800; //测试目标
                //双dest_long = 72.8545;
                双dest_lat = gp2.getLatitudeE6()/ 1E6; //测试目标
                双dest_long = gp2.getLongitudeE6()/ 1E6;
                的GeoPoint GP1 =新的GeoPoint((INT)(src_lat * 1E6),(INT)(src_long * 1E6));
                DrawPath(GP1,GP2,Color.GREEN,图形页面);
            }
            返回true;
        }
    }

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        图形页面图形页面=(图形页面)findViewById(R.id.myMapView1);
        的LinearLayout变焦=(的LinearLayout)findViewById(R.id.myMapView);
        查看ZOOMVIEW = mapView.getZoomControls();

        双src_lat = 19.0552; //测试源
        双src_long = 72.8308;
        GP1 =新的GeoPoint(
        (INT)(src_lat * 1E6)
        (中间体)(src_long * 1E6));

        MyOverLay mapOverlay =新MyOverLay();
        名单<覆盖> listOfOverlays =调用MapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();
        MC = mapView.getController();
        mc.animateTo(GP1);
        mc.setZoom(17);
        mapView.invalidate();

        。mapView.getController()animateTo(GP1);
        mapView.getController()setZoom(15)。
    }

    私人无效DrawPath(SRC的GeoPoint,GeoPoint对象DEST,诠释色彩,图形页面mMapView01){
        //Toast.makeText(getBaseContext(),"Drawpath初始化,Toast.LENGTH_SHORT).show();
        //连接到地图Web服务
        StringBuilder的urlString =新的StringBuilder();
        urlString.append(http://maps.google.com/maps?f=d&hl=en);
        urlString.append(&放大器; SADDR =); //从
        urlString.append(Double.toString((双)src.getLatitudeE6()/ 1.0E6));
        urlString.append(,);
        urlString.append(Double.toString((双)src.getLongitudeE6()/ 1.0E6));
        urlString.append(&放大器; DADDR =); //给
        urlString.append(Double.toString((双)dest.getLatitudeE6()/ 1.0E6));
        urlString.append(,);
        urlString.append(Double.toString((双)dest.getLongitudeE6()/ 1.0E6));
        urlString.append(&放大器,即= UTF8&功放; 0安培; OM = 0&放大器;输出= KML);
        //Toast.makeText(getBaseContext(),urlString,Toast.LENGTH_LONG).show();
        Log.d(XXX,URL =+ urlString.toString());
        //获取KML(XML)文档。并解析它来获得坐标(方向线)。
        文档DOC = NULL;
        HttpURLConnection类的URLConnection = NULL;
        网址URL = NULL;

        尝试 {
            URL =新的URL(urlString.toString());
            的URLConnection =(HttpURLConnection类)url.openConnection();
            urlConnection.setRequestMethod(GET);
            urlConnection.setDoOutput(真正的);
            urlConnection.setDoInput(真正的);
            //Toast.makeText(getBaseContext(),"connected,Toast.LENGTH_SHORT).show();
            urlConnection.connect();
            //Toast.makeText(getBaseContext(),"connected,Toast.LENGTH_SHORT).show();
            DocumentBuilderFactory的DBF = DocumentBuilderFactory.newInstance();
            DocumentBuilder的DB = dbf.newDocumentBuilder();
            文档= db.parse(urlConnection.getInputStream());

            如果(doc.getElementsByTagName(GeometryCollection的)的getLength()> 0){
                //字符串路径= doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName();
                字符串路径= doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue();
                Log.d(XXX,路径=+路径);
                的String []对= path.split();
                串[] lngLat =双[0] .split(,); // lngLat [0] =经度lngLat [1] =纬度lngLat [2] =高度
                // SRC
                的GeoPoint startGP =新的GeoPoint((int)的(Double.parseDouble(lngLat [1])* 1E6),(int)的(Double.parseDouble(lngLat [0])* 1E6));
                。mMapView01.getOverlays()增加(新MyOverLay(startGP,startGP,1));
                的GeoPoint GP1;
                的GeoPoint GP2 = startGP;
                的for(int i = 1; I< pairs.length;我++){//最后一个会崩溃
                    lngLat =对[I] .split(,);
                    GP1 = GP2;
                    // 小心!对于GeoPoint对象,第一:纬度,第二:经度
                    GP2 =新的GeoPoint((int)的(Double.parseDouble(lngLat [1])* 1E6),(int)的(Double.parseDouble(lngLat [0])* 1E6));
                    。mMapView01.getOverlays()增加(新MyOverLay(GP1,gp2,2,颜色));
                    Log.d(XXX,对:+对[I]);
                }
                。mMapView01.getOverlays()增加(新MyOverLay(DEST,DEST,3)); //使用默认颜色
            }
        }
        赶上(MalformedURLException的E){
            e.printStackTrace();
        }
        赶上(IOException异常E){
            e.printStackTrace();
        }
        赶上(的ParserConfigurationException E){
            e.printStackTrace();
        }
        赶上(的SAXException E){
            e.printStackTrace();
        }
    }

    @覆盖
    保护的布尔isRouteDisplayed(){
        // TODO自动生成方法存根
        返回true;
    }
}
 

我已经经历了所有的建议链接N答案不见了,来到这个阶段,其中我能够计算距离,但不能绘制的路线,所有我能得到的是最后两个点的KML文件之间的直线 这里是我的code .....好心还是让我知道我可以克服这是我必须从源到目的地绘制完整的行驶路线。

 公共类RoutePath扩展MapActivity {
的GeoPoint GP1;
的GeoPoint GP2;
的GeoPoint srcGeoPoint;
的GeoPoint destGeoPoint;
双距离;

公共类MyOverLay扩展com.google.android.maps.Overlay
{

    私人诠释mRadius = 6;
    私人诠释模式= 0;
    私人诠释defaultColor;
    公共MyOverLay()
    {

    }

    公共MyOverLay(的GeoPoint P1,P2的GeoPoint,INT模式)// GeoPoint对象是一个int类型。 (6E)
    {
        GP1 = P1;
        GP2 = P2;
        this.mode =模式;
        defaultColor = 999; //没有defaultColor
    }

    公共MyOverLay(的GeoPoint P1,P2的GeoPoint,诠释方式,诠释defaultColor)
    {
        GP1 = P1;
        GP2 = P2;
        this.mode =模式;
        this.defaultColor = defaultColor;

    }

    公众诠释获取模式()
    {
        返回模式;
    }

    @覆盖
    公共布尔平局(画布油画,图形页面图形页面,布尔影子,时长)
    {
        投影投影= MapView.getProjection()在;
        如果(影子==假)
        {
            涂料粉刷=新的油漆();
            paint.setAntiAlias​​(真正的);
            点对点=新的点();
            projection.toPixels(GP1,点);
            //模式= 1&安培;#65306;启动
            如果(模式== 1)
            {
                如果(defaultColor == 999)
                    paint.setColor(Color.RED);
                //Toast.makeText(getBaseContext(),模式1,Toast.LENGTH_SHORT).show();
                其他
                    paint.setColor(defaultColor);
                RectF椭圆形=新RectF(point.x  -  mRadius,point.y  -  mRadius,point.x + mRadius,point.y + mRadius);
                //启动点
                canvas.drawOval(椭圆形,油漆);

            }
            //模式= 2及#65306;路径
            如果(模式== 2)
            {
                如果(defaultColor == 999)
                    paint.setColor(Color.RED);
                //Toast.makeText(getBaseContext(),模式2,Toast.LENGTH_SHORT).show();
                其他
                paint.setColor(defaultColor);
                点点2 =新的点();
                projection.toPixels(GP2,点2);
                paint.setStrokeWidth(5);
                paint.setAlpha(120);
                canvas.drawLine(point.x,point.y,point2.x,point2.y,油漆);

            }
            / *模式= 3安培;#65306;末* /
            否则,如果(模式== 3)
            {
                //最后一个路径

                如果(defaultColor == 999)
                    {paint.setColor(Color.BLUE);
                Toast.makeText(getBaseContext(),模式3,Toast.LENGTH_SHORT).show();}
                其他
                    paint.setColor(defaultColor);
                点点2 =新的点();
                projection.toPixels(GP2,点2);
                paint.setStrokeWidth(5);
                paint.setAlpha(120);
                canvas.drawLine(point.x,point.y,point2.x,point2.y,油漆);
                RectF椭圆形=新RectF(point2.x  -  mRadius,point2.y  -  mRadius,point2.x + mRadius,point2.y + mRadius);
                paint.setAlpha(255);
                canvas.drawOval(椭圆形,油漆);

            }
        }

        返回super.draw(帆布,图形页面,阴影,时);
    }

    @覆盖
    公共布尔的onTouchEvent(MotionEvent事件,图形页面图形页面)
    {
        // ---当用户抬起手指---
        如果(event.getAction()== 1)
        {
            的GeoPoint destGeoPoint = MapView.getProjection()在。fromPixels(
                (int)的event.getX(),
                (int)的event.getY());

            Toast.makeText(getBaseContext(),
                    destGeoPoint.getLatitudeE6()/ 1E6 +,+
                    destGeoPoint.getLongitudeE6()/ 1E6,
                    Toast.LENGTH_SHORT).show();


           双src_lat = 19.0552; //测试源
            双src_long = 72.8308;

            双dest_lat = destGeoPoint.getLatitudeE6()/ 1E6; //测试目标
            双dest_long = destGeoPoint.getLongitudeE6()/ 1E6;
            GP1 =新的GeoPoint((INT)(src_lat * 1E6),(INT)(src_long * 1E6));
            GP2 =新的GeoPoint((INT)(dest_lat * 1E6),(INT)(dest_long * 1E6));

            地理codeR地理codeR =新的地缘codeR(
                    getBaseContext(),Locale.getDefault());
                尝试 {
                    名单<地址>地址=地理coder.getFromLocation(
                        gp2.getLatitudeE6()/ 1E6,
                        gp2.getLongitudeE6()/ 1E6,1);

                    字符串添加=「;
                    如果(addresses.size()大于0)
                    {
                        的for(int i = 0; I< addresses.get(0).getMaxAddressLineIndex();
                             我++)
                           添加+ = addresses.get(0).getAddressLine(我)+\ N的;
                    }

                    Toast.makeText(getBaseContext(),添加,Toast.LENGTH_SHORT).show();

                   DrawPath(GP1,GP2,Color.GREEN,图形页面);
                }
                赶上(IOException异常E){
                    e.printStackTrace();
                }
                返回true;
        }
        其他
                返回false;
    }




}




/ **第一次创建活动时调用。 * /

图形页面图形页面;

@覆盖
公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    图形页面图形页面=(图形页面)findViewById(R.id.myMapView1);
    mapView.setBuiltInZoomControls(真正的);
    双src_lat = 19.0552; //测试源
    双src_long = 72.8308;

    GP1 =新的GeoPoint(
            (INT)(src_lat * 1E6)
            (中间体)(src_long * 1E6));

    MyOverLay mapOverlay =新MyOverLay();
        名单<覆盖> listOfOverlays =调用MapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);

    mapView.invalidate();
    mapView.setSatellite(真正的);

    。mapView.getController()animateTo(GP1);
    mapView.getController()setZoom(15)。

}

@覆盖
保护布尔isRouteDisplayed()
{
// TODO自动生成方法存根
    返回true;
}



私人无效DrawPath(SRC的GeoPoint,GeoPoint对象DEST,诠释色彩,图形页面mMapView01)
{
    双距离1 = 0;
    //连接到地图Web服务
    StringBuilder的urlString =新的StringBuilder();
    urlString.append(http://maps.google.com/maps?f=d&hl=en);
    urlString.append(&放大器; SADDR =); //从
    urlString.append(Double.toString((双)src.getLatitudeE6()/ 1.0E6));
    urlString.append(,);
    urlString.append(Double.toString((双)src.getLongitudeE6()/ 1.0E6));
    urlString.append(&放大器; DADDR =); //给
    urlString.append(Double.toString((双)dest.getLatitudeE6()/ 1.0E6));
    urlString.append(,);
    urlString.append(Double.toString((双)dest.getLongitudeE6()/ 1.0E6));
    urlString.append(&放大器,即= UTF8&功放; 0安培; OM = 0&放大器;输出= KML);
    Log.d(XXX,URL =+ urlString.toString());
    //获取KML(XML)文档。并解析它来获得坐标(方向线)。
    文档DOC = NULL;
    HttpURLConnection类的URLConnection = NULL;
    网址URL = NULL;
    尝试
    {
        URL =新的URL(urlString.toString());
        的URLConnection =(HttpURLConnection类)url.openConnection();
        urlConnection.setRequestMethod(GET);
        urlConnection.setDoOutput(真正的);
        urlConnection.setDoInput(真正的);
        // Toast.makeText(getBaseContext(),连接前,Toast.LENGTH_SHORT).show();
        urlConnection.connect();
        // Toast.makeText(getBaseContext(),连接后,Toast.LENGTH_SHORT).show();

        DocumentBuilderFactory的DBF = DocumentBuilderFactory.newInstance();
        DocumentBuilder的DB = dbf.newDocumentBuilder();
        文档= db.parse(urlConnection.getInputStream());

        如果(doc.getElementsByTagName(GeometryCollection的)的getLength()> 0)
        {


            字符串路径= doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue() ;
            Log.d(XXX,路径=+路径);
            的String []对= path.split();
            串[] lngLat =双[0] .split(,); // lngLat [0] =经度lngLat [1] =纬度lngLat [2] =高度
            // SRC
            的GeoPoint startGP =新的GeoPoint((int)的(Double.parseDouble(lngLat [1])* 1E6),(int)的(Double.parseDouble(lngLat [0])* 1E6));
            //mMapView01.getOverlays().add(new MyOverLay(源,SRC,1));
            的GeoPoint GP1;
            的GeoPoint GP2 = startGP;



            //Toast.makeText(getBaseContext(),连接前,Toast.LENGTH_SHORT).show();
            的for(int i = 1; I< pairs.length;我++)//最后一个会崩溃
            {
                位置locationA =新的位置(A点);
                locationA.setLatitude(startGP.getLatitudeE6()/ 1E6);
                locationA.setLongitude(startGP.getLongitudeE6()/ 1E6);

                lngLat =对[I] .split(,);
                GP1 = GP2;

                GP2 =新的GeoPoint((int)的(Double.parseDouble(lngLat [1])* 1E6),(int)的(Double.parseDouble(lngLat [0])* 1E6));
                。mMapView01.getOverlays()增加(新MyOverLay(GP1,gp2,2,颜色));


                mMapView01.invalidate();
                位置locationB =新的位置(点B);
                locationB.setLatitude(gp2.getLatitudeE6()/ 1E6);
                locationB.setLongitude(gp2.getLongitudeE6()/ 1E6);

               距离1 = + locationA.distanceTo(locationB);

                Log.d(XXX,对:+对[I]);

            }


            距离=距离1/1000;
            如果(距离< = 5.00)
            {Toast.makeText(getBaseContext(),距离=+距离,Toast.LENGTH_SHORT).show();}
            其他
            {Toast.makeText(getBaseContext(),位置超出范围,Toast.LENGTH_SHORT).show();}
        }
    }
    赶上(MalformedURLException的E)
    {
        e.printStackTrace();
    }
    赶上(IOException异常E)
    {
        e.printStackTrace();
    }
    赶上(的ParserConfigurationException E)
    {
        e.printStackTrace();
    }
    赶上(的SAXException E)
    {
        e.printStackTrace();
    }
}

}
 

解决方案

你必须看看下面的帖子可能解决您的问题。

的http://www.anddev.org/the_friend_finder_-_mapactivity_using_gps_-_part_i_-_ii-t93.html

编辑: J2ME/Android/BlackBerry - 行车路线,之间的两个位置路线

我有车app下载 我有车电脑版下载

这是非常描述后解决您的问题,尝试解决方案贴有

Hey I'm trying this code to get connected to Google maps by passing source and destination co-ordinates to this URL and receive a KML file. However I'm not able to parse or get connected to this URL.

Can anybody see where I'm going wrong?

public class RoutePath extends MapActivity {
    MapView mapView;
    GeoPoint gp1,gp2;
    MapController mc;

    public class MyOverLay extends Overlay {
        private int mRadius=6;
        private int mode=0;
        private int defaultColor;
        private String text="";
        private Bitmap img = null;

        public MyOverLay() {
        }

        public MyOverLay(GeoPoint p1,GeoPoint p2,int mode) { // GeoPoint is a int. (6E)
            gp1 = p1;
            gp2 = p2;
            this.mode = mode;
            defaultColor = 999; // no defaultColor
        }

        public MyOverLay(GeoPoint p1,GeoPoint p2,int mode, int defaultColor) {
            gp1 = p1;
            gp2 = p2;
            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) {
            super.draw(canvas, mapView, shadow);   
            Point screenPts = new Point();
            mapView.getProjection().toPixels(gp1, screenPts);
            Bitmap bmp = BitmapFactory.decodeResource(
            getResources(), R.drawable.pushpin);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null); 
            return true;
        }

        public boolean onTouchEvent(MotionEvent event, MapView mapView) {
            // when user lifts his finger 
            if (event.getAction() == 1) {
                Toast.makeText(getBaseContext(),"Distance", Toast.LENGTH_SHORT).show(); 
                GeoPoint gp2 = mapView.getProjection().fromPixels(
                (int) event.getX(),
                (int) event.getY());

                Toast.makeText(getBaseContext(), gp2.getLatitudeE6() / 1E6 + "," + gp2.getLongitudeE6() /1E6 , Toast.LENGTH_SHORT).show();

                double src_lat = 19.0552; // the testing source
                double src_long =  72.8308;
                //double dest_lat = 19.0800; // the testing destination
                //double dest_long = 72.8545;
                double dest_lat = gp2.getLatitudeE6()  / 1E6; // the testing destination
                double dest_long = gp2.getLongitudeE6() /1E6;
                GeoPoint gp1 = new GeoPoint((int) (src_lat * 1E6),(int) (src_long * 1E6));
                DrawPath(gp1, gp2, Color.GREEN, mapView);
            }
            return true;
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapView mapView = (MapView) findViewById(R.id.myMapView1);
        LinearLayout zoom = (LinearLayout)findViewById(R.id.myMapView);  
        View zoomView = mapView.getZoomControls();

        double src_lat = 19.0552; // the testing source
        double src_long =  72.8308;
        gp1 = new GeoPoint(
        (int) (src_lat * 1E6), 
        (int) (src_long * 1E6));

        MyOverLay mapOverlay = new MyOverLay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();
        mc = mapView.getController();
        mc.animateTo(gp1);
        mc.setZoom(17); 
        mapView.invalidate();

        mapView.getController().animateTo(gp1);
        mapView.getController().setZoom(15);
    }

    private void DrawPath(GeoPoint src,GeoPoint dest, int color, MapView mMapView01) {
        //Toast.makeText(getBaseContext(),"Drawpath initialized", Toast.LENGTH_SHORT).show(); 
        // connect to map web service
        StringBuilder urlString = new StringBuilder();
        urlString.append("http://maps.google.com/maps?f=d&hl=en");
        urlString.append("&saddr=");//from
        urlString.append( Double.toString((double)src.getLatitudeE6()/1.0E6 ));
        urlString.append(",");
        urlString.append( Double.toString((double)src.getLongitudeE6()/1.0E6 ));
        urlString.append("&daddr=");//to
        urlString.append( Double.toString((double)dest.getLatitudeE6()/1.0E6 ));
        urlString.append(",");
        urlString.append( Double.toString((double)dest.getLongitudeE6()/1.0E6 ));
        urlString.append("&ie=UTF8&0&om=0&output=kml");
        //Toast.makeText(getBaseContext(),urlString, Toast.LENGTH_LONG).show(); 
        Log.d("xxx","URL="+urlString.toString());
        // get the kml (XML) doc. And parse it to get the coordinates(direction route).
        Document doc = null;
        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);
            //Toast.makeText(getBaseContext(),"connected", Toast.LENGTH_SHORT).show();
            urlConnection.connect(); 
            //Toast.makeText(getBaseContext(),"connected", Toast.LENGTH_SHORT).show(); 
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(urlConnection.getInputStream()); 

            if(doc.getElementsByTagName("GeometryCollection").getLength()>0) {
                //String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getNodeName();
                String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue();
                Log.d("xxx","path="+ path);
                String [] pairs = path.split(" "); 
                String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude lngLat[1]=latitude lngLat[2]=height
                // src
                GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
                mMapView01.getOverlays().add(new MyOverLay(startGP,startGP,1));
                GeoPoint gp1;
                GeoPoint gp2 = startGP; 
                for(int i=1;i<pairs.length;i++) { // the last one would be crash
                    lngLat = pairs[i].split(",");
                    gp1 = gp2;
                    // watch out! For GeoPoint, first:latitude, second:longitude
                    gp2 = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
                    mMapView01.getOverlays().add(new MyOverLay(gp1,gp2,2,color));
                    Log.d("xxx","pair:" + pairs[i]);
                }
                mMapView01.getOverlays().add(new MyOverLay(dest,dest, 3)); // use the default color
            } 
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        catch (SAXException e) {
            e.printStackTrace();
        }
    }

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

I have gone through all the suggested links n answers and have come to this stage wherein i am able to calculate distance but can not draw route, all i can get is a straight line between the last two points of the kml file here is my code.....kindly lemme know how can i overcome this as i have to draw the complete travelling route from source to destination.

public class RoutePath extends MapActivity {
GeoPoint gp1;
GeoPoint gp2;
GeoPoint srcGeoPoint;
GeoPoint destGeoPoint;
double distance;

public class MyOverLay extends com.google.android.maps.Overlay
{

    private int mRadius=6;
    private int mode=0;
    private int defaultColor;
    public MyOverLay()
    {

    }

    public MyOverLay(GeoPoint p1,GeoPoint p2,int mode) // GeoPoint is a int. (6E)
    {
        gp1 = p1;
        gp2 = p2;
        this.mode = mode;
        defaultColor = 999; // no defaultColor
    }

    public MyOverLay(GeoPoint p1,GeoPoint p2,int mode, int defaultColor)
    {
        gp1 = p1;
        gp2 = p2;
        this.mode = mode;
        this.defaultColor = defaultColor ;

    }

    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.RED);
                //Toast.makeText(getBaseContext(), "mode1", Toast.LENGTH_SHORT).show();
                else
                    paint.setColor(defaultColor);
                RectF oval=new RectF(point.x - mRadius, point.y - mRadius,point.x + mRadius, point.y + mRadius);
                // start point
                canvas.drawOval(oval, paint);

            }
            // mode=2&#65306;path
            if(mode==2)
            {
                if(defaultColor==999)
                    paint.setColor(Color.RED);
                //Toast.makeText(getBaseContext(), "mode2", Toast.LENGTH_SHORT).show();
                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.BLUE);
                Toast.makeText(getBaseContext(), "mode3", Toast.LENGTH_SHORT).show();}
                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);
                RectF oval=new RectF(point2.x - mRadius,point2.y - mRadius,point2.x + mRadius,point2.y + mRadius);                      
                paint.setAlpha(255);
                canvas.drawOval(oval, paint);

            }
        }

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

    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    {   
        //---when user lifts his finger---
        if (event.getAction() == 1)
        {                
            GeoPoint destGeoPoint = mapView.getProjection().fromPixels(
                (int) event.getX(),
                (int) event.getY());

            Toast.makeText(getBaseContext(), 
                    destGeoPoint.getLatitudeE6() / 1E6 + "," + 
                    destGeoPoint.getLongitudeE6() /1E6 , 
                    Toast.LENGTH_SHORT).show();


           double src_lat = 19.0552; // the testing source
            double src_long =  72.8308;

            double dest_lat = destGeoPoint.getLatitudeE6()  / 1E6; // the testing destination
            double dest_long = destGeoPoint.getLongitudeE6() /1E6;
            gp1 = new GeoPoint((int) (src_lat * 1E6),(int) (src_long * 1E6));
            gp2 = new GeoPoint((int) (dest_lat * 1E6),(int) (dest_long * 1E6));

            Geocoder geoCoder = new Geocoder(
                    getBaseContext(), Locale.getDefault());
                try {
                    List<Address> addresses = geoCoder.getFromLocation(
                        gp2.getLatitudeE6()  / 1E6, 
                        gp2.getLongitudeE6() / 1E6, 1);

                    String add = "";
                    if (addresses.size() > 0) 
                    {
                        for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
                             i++)
                           add += addresses.get(0).getAddressLine(i) + "\n";
                    }

                    Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();

                   DrawPath(gp1, gp2 , Color.GREEN, mapView); 
                }
                catch (IOException e) {                
                    e.printStackTrace();
                }   
                return true;
        }
        else                          
                return false;
    }  




}




/** Called when the activity is first created. */

MapView mapView;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MapView mapView = (MapView) findViewById(R.id.myMapView1);
    mapView.setBuiltInZoomControls(true);
    double src_lat = 19.0552; // the testing source
    double src_long =  72.8308;

    gp1 = new GeoPoint(
            (int) (src_lat * 1E6), 
            (int) (src_long * 1E6));

    MyOverLay mapOverlay = new MyOverLay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);

    mapView.invalidate();
    mapView.setSatellite(true);

    mapView.getController().animateTo(gp1);
    mapView.getController().setZoom(15);

}

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



private void DrawPath(GeoPoint src,GeoPoint dest, int color, MapView mMapView01)
{
    double distance1=0;
    // connect to map web service
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.google.com/maps?f=d&hl=en");
    urlString.append("&saddr=");//from
    urlString.append( Double.toString((double)src.getLatitudeE6()/1.0E6 ));
    urlString.append(",");
    urlString.append( Double.toString((double)src.getLongitudeE6()/1.0E6 ));
    urlString.append("&daddr=");//to
    urlString.append( Double.toString((double)dest.getLatitudeE6()/1.0E6 ));
    urlString.append(",");
    urlString.append( Double.toString((double)dest.getLongitudeE6()/1.0E6 ));
    urlString.append("&ie=UTF8&0&om=0&output=kml");
    Log.d("xxx","URL="+urlString.toString());
    // get the kml (XML) doc. And parse it to get the coordinates(direction route).
    Document doc = null;
    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);
        // Toast.makeText(getBaseContext(), "Before Connection", Toast.LENGTH_SHORT).show();
        urlConnection.connect(); 
        // Toast.makeText(getBaseContext(), "After Connection", Toast.LENGTH_SHORT).show();

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(urlConnection.getInputStream()); 

        if(doc.getElementsByTagName("GeometryCollection").getLength()>0)
        {


            String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue() ;
            Log.d("xxx","path="+ path);
            String [] pairs = path.split(" "); 
            String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude lngLat[1]=latitude lngLat[2]=height
            // src
            GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
            //mMapView01.getOverlays().add(new MyOverLay(src,src,1));
            GeoPoint gp1;
            GeoPoint gp2 = startGP;



            //Toast.makeText(getBaseContext(), "Before Connection", Toast.LENGTH_SHORT).show();
            for(int i=1;i<pairs.length;i++) // the last one would be crash
            {
                Location locationA = new Location("Point A");
                locationA.setLatitude(startGP.getLatitudeE6()/1E6);
                locationA.setLongitude(startGP.getLongitudeE6()/1E6);

                lngLat = pairs[i].split(",");
                gp1 = gp2;

                gp2 = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
                mMapView01.getOverlays().add(new MyOverLay(gp1,gp2,2,color));


                mMapView01.invalidate();
                Location locationB = new Location("Point B");
                locationB.setLatitude(gp2.getLatitudeE6()/1E6);
                locationB.setLongitude(gp2.getLongitudeE6()/1E6);

               distance1 = + locationA.distanceTo(locationB);

                Log.d("xxx","pair:" + pairs[i]);

            }


            distance = distance1/1000;
            if(distance<= 5.00)
            {Toast.makeText(getBaseContext(), "DISTANCE = "+ distance, Toast.LENGTH_SHORT).show();}
            else
            {Toast.makeText(getBaseContext(), "Location out of range",Toast.LENGTH_SHORT).show();}
        } 
    }
    catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (ParserConfigurationException e)
    {
        e.printStackTrace();
    }
    catch (SAXException e)
    {
        e.printStackTrace();
    }
}

}

解决方案

you must look into following post probably solves your problem

http://www.anddev.org/the_friend_finder_-_mapactivity_using_gps_-_part_i_-_ii-t93.html

Edit: J2ME/Android/BlackBerry - driving directions, route between two locations

This is very descriptive post addressing your problem try solution posted there