使用Google API从我的位置绘制路线到目的地位置

时间:2012-05-17 06:32:38

标签: android google-maps routes

我试图制作谷歌地图使用位置,我得到我的位置的lon,lat。 这是我的代码

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
Location myLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
String uri = "http://maps.google.com/maps?saddr=" + (myLocation.getLatitude())+","+(myLocation.getLongitude())+"&daddr="+destLat+","+destLon;

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 个答案:

答案 0 :(得分:3)

以下是https://github.com/frogermcs/RoutePathExample的完整源代码,介绍如何在两个地理位置之间绘制路径。我毫不费力地使用这段代码。你应该检查一下

答案 1 :(得分:3)

试试这个

private boolean gps_enabled = false;
private boolean network_enabled = false;
private LocationListener locListener = new MyLocationListener();
LocationManager locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    try {
        gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
    }
    try {
        network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
    }

    // don't start listeners if no provider is enabled
    if (!gps_enabled && !network_enabled) {

    }

    if (gps_enabled) {
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 10, locListener);
    }
    if (network_enabled) {
        locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 10, locListener);
    }

class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {

            currentLocation = location;


    }



    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }


    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }


    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

然后,一旦获得位置,就可以使用

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("http://maps.google.com/maps?saddr=<start lat>,<start lon>&daddr=<dest lat>,<dest lon>"));
startActivity(intent);