使用 "Google-Directions-Android" 库不会在地图上绘制路线

Route won't draw on map with "Google-Directions-Android" library

我正在使用 Google-Directions-Android Library (while following this example).
我已经按照上面的示例进行操作,但地图上没有为我绘制路线 - 也没有抛出任何错误,我不知道是什么我做错了。

我的地图代码(示例中的代码大部分在下面"onRoutingCancelled" - 在底部。):

public class mapFragment extends Fragment implements OnMapReadyCallback , RoutingListener{
GoogleMap mGoogleMAp;
MapView mMapView;
View mView;
Double mLatitude = 32.109333;
Double mLongitude = 34.855499;
MyLocation myLocation;
MyLocation.LocationResult locationResult;
CameraPosition cameraPosition;
Context context123;
private List<Polyline> polylines;
private static final int[] COLORS = new int[]{R.color.secondaryColor};

public mapFragment() {
}

public void changeCordinate(Double latitude, Double longitude, Context mContext) {
    mLatitude = latitude;
    mLongitude = longitude;
    this.context123 = mContext;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myLocation = new MyLocation();
    //to start the polyline
    polylines = new ArrayList<>();
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.fragment_map_fragment, container, false);
    return mView;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    mMapView = mView.findViewById(R.id.map);
    if (mMapView != null) {
        mMapView.onCreate(null);
        mMapView.onResume();
        mMapView.getMapAsync(this);

        locationResult = new MyLocation.LocationResult() {
            @Override
            public void gotLocation(Location location) {
                //when i have the user current location start show his location on the map
                MapsInitializer.initialize(getContext());
                //map tye(animated , looks like real map etc...)
                mGoogleMAp.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                //adds marker in map according to the current user position
                //first add marker object
                MarkerOptions marker = new MarkerOptions();
                //to the above marker object - add information
                mGoogleMAp.addMarker(marker
                        .position(new LatLng(location.getLatitude(), location.getLongitude()))
                        .snippet("look like nice place"))
                        .setTitle("this is me");
                //todo check later on why this wont change my map icon
                //marker.icon((BitmapDescriptorFactory.fromResource(R.drawable.ic_car_icon)));

                /**add polyline start**/
                // Add polyline and polygons to the map. This section shows just
                Polyline polyline1 = mGoogleMAp.addPolyline(new PolylineOptions()
                        .clickable(true)
                        .add(
                              //  new LatLng(31.785964, 34.704885),
                                // new LatLng(location.getLatitude(), location.getLongitude()),
                                new LatLng(32.111846, 34.804672),
                                new LatLng(31.877202, 34.740008)));
                getRout((new LatLng(31.877202, 34.740008)),new LatLng(32.111846, 34.804672));

                /**add polyline end***/
                //move the camera to the current position
                cameraPosition = CameraPosition.builder()
                        .target(new LatLng(location.getLatitude(), location.getLongitude()))
                        .zoom(16)
                        .bearing(0)
                        .tilt(45)
                        .build();
                mGoogleMAp.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            }
        };
    }
}


@Override
public void onMapReady(final GoogleMap googleMap) {
    mGoogleMAp = googleMap;
    myLocation.getLocation(getContext(), locationResult);
    mapConfig(googleMap);
}

//a method ot add polyline to google maps
public void addPolyline(){
    Polyline line = mGoogleMAp.addPolyline(
            new PolylineOptions().add(
                    new LatLng(mLatitude, mLongitude),
                    new LatLng(32.113618, 34.804972)
            ).width(2).color(Color.BLUE).geodesic(true)
    );
}

private void mapConfig(GoogleMap googleMap){
    // TODO: 30/10/2018 make sure that the user uproved location permission 
  //  googleMap.setMyLocationEnabled(true); // false to disable
    googleMap.getUiSettings().setZoomControlsEnabled(true); // true to enable
    googleMap.getUiSettings().setZoomGesturesEnabled(true);
    googleMap.getUiSettings().setCompassEnabled(true);
    googleMap.getUiSettings().setMyLocationButtonEnabled(true);
    googleMap.getUiSettings().setRotateGesturesEnabled(true);
}

/**************************start listeners for rout*************************************************************/
@Override
public void onRoutingFailure(RouteException e) {
    if(e != null) {
        System.out.println("you got rout error " + e.getMessage());
    }
}
@Override
public void onRoutingStart() {
}
@Override
public void onRoutingSuccess(ArrayList<Route> route, int shortestRoutIndex) {
    if(polylines.size()>0) {
        for (Polyline poly : polylines) {
            poly.remove();
        }
    }
    polylines = new ArrayList<>();
    //add route(s) to the map.
    for (int i = 0; i <route.size(); i++) {
        //In case of more than 5 alternative routes
        int colorIndex = i % COLORS.length;
        PolylineOptions polyOptions = new PolylineOptions();
        polyOptions.color(getResources().getColor(COLORS[colorIndex]));
        polyOptions.width(10 + i * 3);
        polyOptions.addAll(route.get(i).getPoints());
        Polyline polyline = mGoogleMAp.addPolyline(polyOptions);
        polylines.add(polyline);

        Toast.makeText(context123,"Route "+ (i+1) +": distance - "+ route.get(i).getDistanceValue()+": duration - "+ route.get(i).getDurationValue(),Toast.LENGTH_SHORT).show();

    }
}
@Override
public void onRoutingCancelled() {
}
/*************************end listeners for routend listeners for rout*************************************************************/

//a method to delete the polylines
private void deletePolyline(){
    for(Polyline line : polylines){
        line.remove();
    }
    polylines.clear();
}
//get rout to specific marker
private void getRout(LatLng start,LatLng end){
    start = new LatLng(18.015365, -77.499382);
   LatLng waypoint= new LatLng(18.01455, -77.499333);
    end = new LatLng(18.012590, -77.500659);

    Routing routing = new Routing.Builder()
            .travelMode(Routing.TravelMode.DRIVING)
            .withListener(this)
            .waypoints(start, waypoint, end)
            .build();
    routing.execute();
    }
}

最后,我通过从 google API 控制台添加服务器密钥解决了这个问题,为了使用 google 方向,您必须输入正确的密钥。