路线不会在地图上绘制“Google-Directions-Android”库

问题描述 投票:1回答:1

我正在使用Google-Directions-Android Library(同时关注 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();
    }
}
android google-maps google-direction
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.