1

我试图制作一个使用位置的谷歌地图,我得到了我的位置的 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);

它运行但什么也没有发生。只是显示我的位置。谁能告诉我声明中的错误在哪里?请

4

2 回答 2

3

这是https://github.com/frogermcs/RoutePathExample上的完整源代码,了解如何在两个地理位置之间绘制路径。我使用这段代码完全没有任何问题。你应该检查一下

于 2012-05-17T06:50:25.793 回答
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);
于 2012-05-17T07:47:32.683 回答