在android中的两个位置之间的地图上查找路线

时间:2012-05-24 06:14:24

标签: android google-maps

我正在构建一个应用程序,我需要在两个位置之间的地图上找到路线。用户可以输入2个位置,即源和目的地,他可以在地图上获取路线。 有人可以建议我如何继续dis.Is有一个教程,可以帮助我吗? 提前完成。

2 个答案:

答案 0 :(得分:3)

  public class MappzzActivity extends MapActivity {
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapp);
    double src_lat = latPoint;
    double src_long = lngPoint;

    double dest_lat = 8.88056;
    double dest_long = 76.5917;
    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, Color.GREEN, mapView);

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

}

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

private void DrawPath(GeoPoint src, GeoPoint dest, int color,
        MapView mMapView01) {

    // 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);
        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("xxx", "path=" + path);

            String[] pairs = path.split(" ");
            String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude

            // 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++) {
                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));

        }
    } catch (MalformedURLException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    } catch (ParserConfigurationException e) {

        e.printStackTrace();

    } catch (SAXException e) {

        e.printStackTrace();
    }

}
}

答案 1 :(得分:1)

在两个位置之间显示路线的方法很多。

1)打开默认地图应用并显示路线

String uri = "http://maps.google.com/maps?saddr=" + currentLatitude+","+currentLongitude+"&daddr="+fixedLatitude+","+fixedLongitude;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);

2)使用Google API并解析谷歌API中的数据

通过这种方式,您必须遵循此link

相关问题