如何在谷歌地图上显示两个点的完整路线?

时间:2012-04-27 07:44:29

标签: java android google-maps android-mapview

在我的Android应用程序中我有一个MapView,我已经显示路由b / w两个点我实现了代码,但它只显示了一半路线。我试过很多方法,但无法解决它,请帮助我在做错误的地方。有一个屏幕截图两个红点和蓝色路线http://www.flickr.com/photos/77093196@N03/7117948377/
以下是代码:

MapdirectionsActivity.java

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

    MapView mapView;

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

        mapView = (MapView) findViewById(R.id.mapview);
        double src_lat = 17.3667; // the source
        double src_long = 78.4667;
        double dest_lat =  18.9647; // the destination
        double dest_long = 72.8258;
        GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),(int) (src_long * 1E6));
        GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),(int) (dest_long * 1E6));
        DrawPath(srcGeoPoint, destGeoPoint, mapView);
        mapView.getController().animateTo(srcGeoPoint);
        mapView.getController().setZoom(7);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    private void DrawPath(GeoPoint src, GeoPoint dest, MapView mMapView) {

        // 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&om=1&output=kml");
        Log.d("Map directions", "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);
            urlConnection.connect();

            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("Map directions", "path= " + path);
                String[] pairs = path.split(" ");
                String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude
                                                        // lngLat[1]=latitude
                                                        // lngLat[2]=height
                GeoPoint startGP = new GeoPoint(
                        (int) (Double.parseDouble(lngLat[1]) * 1E6),
                        (int) (Double.parseDouble(lngLat[0]) * 1E6));

                mMapView.getOverlays().add(new MyOverLay(startGP, startGP, 1));//starting point
                GeoPoint gp1 = null;
                GeoPoint gp2 = startGP;
                for (int i = 1; i <pairs.length; i++)
                {
                    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));

                    mMapView.getOverlays().add(new MyOverLay(gp1, gp2, 2));//route
                }

                mMapView.getOverlays().add(new MyOverLay(dest, dest, 3));   //last point
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }
}

MyOverLay.java

public class MyOverLay extends Overlay {
    private GeoPoint gp1;
    private GeoPoint gp2;
    private int mRadius = 6;
    private int mode = 0;

    public MyOverLay(GeoPoint gp1, GeoPoint gp2, int mode) // Constructor
    {
        this.gp1 = gp1;
        this.gp2 = gp2;
        this.mode = mode;
    }

    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);
            // start
            if (mode == 1) {
                paint.setColor(Color.RED);
                RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
                        point.x + mRadius, point.y + mRadius);

                canvas.drawOval(oval, paint);// start point
            }
            // mode=2&#65306;path
            else if (mode == 2) {
                paint.setColor(Color.BLUE);
                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=2&#65306;path
            }
            /* mode=3&#65306;end */
            else if (mode == 3) {
                /* the last path */

                paint.setColor(Color.RED);
                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);/* end point */
            }
        }
        return super.draw(canvas, mapView, shadow, when);
    }

}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="0E2QBBM0fi1TSN6J0hCLbETO8oscApDt5CDqgbQ"
    android:clickable="true"
    android:enabled="true" />

1 个答案:

答案 0 :(得分: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

//同时将权限添加到您的表现文件