Java 无法显示从当前位置到目标google maps V2的路径

Java 无法显示从当前位置到目标google maps V2的路径,java,android,google-maps,Java,Android,Google Maps,我试图用答案画出从当前位置到目的地的路径 但我想用我的真实位置来代替硬编码的当前位置的经纬度。但每当我尝试这样做时,应用程序都会因为行location=mMap.getMyLocation中的错误而崩溃 感谢您的帮助 Java public class MapsActivity extends FragmentActivity { private GoogleMap mMap; // Might be null if Google Play services APK is not av

我试图用答案画出从当前位置到目的地的路径

但我想用我的真实位置来代替硬编码的当前位置的经纬度。但每当我尝试这样做时,应用程序都会因为行location=mMap.getMyLocation中的错误而崩溃

感谢您的帮助

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"));
    }
}
你是说location=mMap.getMyLocation;是代码中的方法getYourLocation,崩溃发生在Location Location=locationManager.getLastKnownLocationprovider

如果是这样,可能会导致NullPointer异常,您需要使用新的api FusedLocationApi来避免getLastLocation null指针

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

对于目的地,您可以查看以了解一些信息


你是说location=mMap.getMyLocation;是代码中的方法getYourLocation,崩溃发生在Location Location=locationManager.getLastKnownLocationprovider?按照给定的三个链接进行操作:谢谢你的回答,但是有没有什么方法可以让我找到在这两个点之间旅行的预计时间和距离?当然可以,请查看,为什么我点击方向时会打开谷歌地图?它不能显示活动本身的路径吗?