无法显示从当前位置到目标谷歌地图V2的路径

时间:2015-04-07 20:02:36

标签: java android google-maps

我正在尝试使用答案Android: How to draw route directions google maps API V2 from current location to destination从当前位置绘制路径到目的地。

但是我想要使用我的真实位置代替当前位置的硬编码I经度和纬度。但每当我尝试这样做时,应用程序在行位置错误崩溃= mMap.getMyLocation();

感谢任何帮助。

MapsActivity.Java

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.
    Location location;
    LatLng myPosition;
    GMapV2Direction md;

    LatLng fromPosition = getYourLocation();
    LatLng toPosition = new LatLng(13.683660045847258, 100.53900808095932);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        md = new GMapV2Direction();
        setUpMapIfNeeded();
        LatLng coordinates = new LatLng(13.685400079263206, 100.537133384495975);
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 16));

        mMap.addMarker(new MarkerOptions().position(fromPosition).title("Start"));
        mMap.addMarker(new MarkerOptions().position(toPosition).title("End"));

        Document doc = md.getDocument(fromPosition, toPosition, GMapV2Direction.MODE_DRIVING);
        int duration = md.getDurationValue(doc);
        String distance = md.getDistanceText(doc);
        String start_address = md.getStartAddress(doc);
        String copy_right = md.getCopyRights(doc);

        ArrayList<LatLng> directionPoint = md.getDirection(doc);
        PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);

        for(int i = 0 ; i < directionPoint.size() ; i++) {
            rectLine.add(directionPoint.get(i));
        }

        mMap.addPolyline(rectLine);
    }


    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever

     * <p/>
     * If it isn't installed {@link SupportMapFragment} (and
     * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
     * install/update the Google Play services APK on their device.
     * <p/>
     * A user can return to this FragmentActivity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
     * have been completely destroyed during this process (it is likely that it would only be
     * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
     * method in {@link #onResume()} to guarantee that it will be called.
     */
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();

            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                /*myPosition = getLocation();
                ZoomCurrentLocation(myPosition);*/
            }
        }
    }

    private LatLng getYourLocation() {
        mMap.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(provider);
        double latitude = 0;
        double longitude = 0;
        if (location != null) {
            // Getting latitude of the current location
            latitude = location.getLatitude();

            // Getting longitude of the current location
            longitude = location.getLongitude();

            // Creating a LatLng object for the current location

        }
        LatLng latLng = new LatLng(latitude, longitude);
        return latLng;
    }


    private void ZoomCurrentLocation(LatLng myPosition)
    {
        mMap.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(provider);

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myPosition, 16));
    }



    private void setUpMap(LatLng myPosition) {
        mMap.addMarker(new MarkerOptions().position(myPosition).title("Marker"));
    }
}

1 个答案:

答案 0 :(得分:0)

您的意思是location = mMap.getMyLocation();是代码中的方法getYourLocation(),崩溃是Location location = locationManager.getLastKnownLocation(provider);吗?

如果是这样,可能由NullPointer Exception导致,您需要使用新的api FusedLocationApi来避免getLastLocation空指针。

请检查here以了解如何使用它。 here是我的github上的代码。

对于目的地,您可以查看here以获得一些想法。

enter image description here