Java Examples for com.google.maps.DirectionsApi

The following java examples will help you to understand the usage of com.google.maps.DirectionsApi. These source code samples are taken from different open source projects.

Example 1
Project: PokeMate-master  File: Navigate.java View source code
private DirectionsStep[] queryDirections(LatLng start, LatLng end) {
    DirectionsStep[] stepsToTake = null;
    GeoApiContext ctx = new GeoApiContext().setApiKey(Config.getGoogleApiKey());
    DirectionsApiRequest request = DirectionsApi.newRequest(ctx).origin(start).destination(end).mode(TravelMode.WALKING);
    try {
        DirectionsResult result = request.await();
        if (result.routes.length > 0) {
            DirectionsRoute directionsRoute = result.routes[0];
            if (directionsRoute.legs.length > 0) {
                DirectionsLeg legs = directionsRoute.legs[0];
                if (legs.steps.length > 0) {
                    stepsToTake = legs.steps;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("Remote Server Exception", e);
    }
    return stepsToTake;
}
Example 2
Project: amos-ss15-proj2-master  File: DirectionsManager.java View source code
/**
     * Find a route using Google's Directions API from a starting point to a destination with serveral
     * given waypoints.
     * @param startLocation the start of the directions query
     * @param endLocation the destination of the query
     * @param waypoints the waypoints that should be visited
     * @return a list of possible routes to the given destination
     */
public List<Route> getDirections(RouteLocation startLocation, RouteLocation endLocation, List<RouteLocation> waypoints) {
    CachingRouteKey cachingRouteKey = new CachingRouteKey(startLocation, endLocation, waypoints);
    if (cachedRoutes.containsKey(cachingRouteKey)) {
        logManager.d("DIRECTIONS REQUEST " + startLocation + " to " + endLocation + " --> USED CACHED RESULT");
        return cachedRoutes.get(cachingRouteKey);
    }
    LatLng origin = new LatLng(startLocation.getLat(), startLocation.getLng());
    LatLng destination = new LatLng(endLocation.getLat(), endLocation.getLng());
    logManager.d("DIRECTIONS REQUEST " + startLocation + " to " + endLocation + " (" + waypoints.size() + " wps)");
    List<LatLng> llWaypoints = new ArrayList<LatLng>();
    if (waypoints.size() > 0) {
        for (RouteLocation loc : waypoints) {
            LatLng waypoint = new LatLng(loc.getLat(), loc.getLng());
            llWaypoints.add(waypoint);
        }
    }
    String[] stringWaypoints = new String[llWaypoints.size()];
    for (int i = 0; i < stringWaypoints.length; ++i) {
        /*GeocodingResult[] result = GeocodingApi.newRequest(geoApiContext).latlng(new LatLng(loc.getLat(), loc.getLng())).await();

            // no route found ...
            if (result == null || result.length == 0) return new ArrayList<>();

            stringWaypoints[i] = result[0].formattedAddress;*/
        LatLng waypoint = llWaypoints.get(i);
        stringWaypoints[i] = waypoint.toUrlValue();
    }
    // create a list containing all the waypoints (also start and destination)
    // don't modify given waypoints list.
    List<RouteLocation> allWaypoints = new ArrayList<RouteLocation>();
    allWaypoints.add(startLocation);
    for (RouteLocation loc : waypoints) allWaypoints.add(loc);
    allWaypoints.add(endLocation);
    List<Route> result = new ArrayList<>();
    DirectionsRoute[] googleRoutes = new DirectionsRoute[0];
    try {
        googleRoutes = DirectionsApi.newRequest(geoApiContext).origin(origin).destination(destination).waypoints(stringWaypoints).await();
        for (DirectionsRoute googleRoute : googleRoutes) {
            result.add(createRoute(allWaypoints, googleRoute));
        }
        cachedRoutes.put(cachingRouteKey, result);
        return result;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Example 3
Project: google-maps-services-java-master  File: DirectionsApiTest.java View source code
@Test
public void testGetDirections() throws Exception {
    DirectionsResult result = DirectionsApi.getDirections(context, "Sydney, AU", "Melbourne, AU").await();
    assertNotNull(result.routes);
    assertNotNull(result.routes[0]);
    assertThat(result.routes[0].overviewPolyline.decodePath().size(), not(0));
    assertTrue(result.routes[0].legs[0].startAddress.startsWith("Sydney NSW"));
    assertTrue(result.routes[0].legs[0].endAddress.startsWith("Melbourne VIC"));
}
Example 4
Project: rox-android-master  File: RecommendedRouteFragment.java View source code
@Override
public Pair<Poi[], DirectionsRoute> call() throws Exception {
    Poi[] nextPois = poisApi.awaitRoute(seed.getFoursquareId());
    List<Poi> pois = new ArrayList<>();
    pois.add(seed);
    pois.addAll(Arrays.asList(nextPois));
    String[] waypoints = new String[pois.size() - 1];
    for (int i = 0; i < waypoints.length; i++) waypoints[i] = toGoogleMapsServicesLatLng(pois.get(i).getLocation());
    DirectionsRoute[] routes = DirectionsApi.newRequest(geoApiContext).origin(toGoogleMapsServicesLatLng(origin)).destination(toGoogleMapsServicesLatLng(pois.get(pois.size() - 1).getLocation())).mode(travelMode).waypoints(waypoints).await();
    return new Pair(nextPois, routes == null || routes.length == 0 ? null : routes[0]);
}
Example 5
Project: penn-mobile-android-master  File: MapFragment.java View source code
private void addWalkingPath(final LatLng startWalking, final LatLng endWalking, final boolean start, final boolean clearOnSuccess) {
    DirectionsApiRequest req = DirectionsApi.getDirections(geoapi, startWalking.latitude + "," + startWalking.longitude, endWalking.latitude + "," + endWalking.longitude);
    req.mode(TravelMode.WALKING);
    req.setCallback(new PendingResult.Callback<DirectionsResult>() {

        @Override
        public void onResult(final DirectionsResult result) {
            activity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    if (clearOnSuccess) {
                        clearMap();
                    }
                    DirectionsRoute[] routes = result.routes;
                    EncodedPolyline polyline = routes[0].overviewPolyline;
                    PolylineOptions options = new PolylineOptions();
                    for (com.google.maps.model.LatLng latLng : polyline.decodePath()) {
                        options.add(new LatLng(latLng.lat, latLng.lng));
                    }
                    options.color(Color.BLUE).width(15);
                    Polyline line = googleMap.addPolyline(options);
                    if (start) {
                        startPolyline = line;
                        allStartPolylines.add(startPolyline);
                    } else {
                        endPolyline = line;
                        allEndPolylines.add(endPolyline);
                    }
                    for (int i = 1; i < routes.length; i++) {
                        PolylineOptions opt = new PolylineOptions();
                        for (com.google.maps.model.LatLng latLng : routes[i].overviewPolyline.decodePath()) {
                            opt.add(new LatLng(latLng.lat, latLng.lng));
                        }
                        opt.color(Color.GRAY).width(15);
                        Polyline poly = googleMap.addPolyline(opt);
                        if (start) {
                            allStartPolylines.add(poly);
                        } else {
                            allEndPolylines.add(poly);
                        }
                    }
                    addGoogleWarning(result);
                    if (clearOnSuccess) {
                        LatLngBounds.Builder bounds = new LatLngBounds.Builder();
                        bounds.include(startWalking);
                        bounds.include(endWalking);
                        changeZoomLevel(googleMap, bounds.build());
                    }
                }
            });
        }

        @Override
        public void onFailure(Throwable e) {
            activity.showErrorToast(R.string.google_map_fail);
        }
    });
}
Example 6
Project: recommendations-master  File: RecommendedRouteFragment.java View source code
@Override
public Pair<Poi[], DirectionsRoute> call() throws Exception {
    Poi[] nextPois = poisApi.awaitRoute(seed.getFoursquareId());
    List<Poi> pois = new ArrayList<>();
    pois.add(seed);
    pois.addAll(Arrays.asList(nextPois));
    String[] waypoints = new String[pois.size() - 1];
    for (int i = 0; i < waypoints.length; i++) waypoints[i] = toGoogleMapsServicesLatLng(pois.get(i).getLocation());
    DirectionsRoute[] routes = DirectionsApi.newRequest(geoApiContext).origin(toGoogleMapsServicesLatLng(origin)).destination(toGoogleMapsServicesLatLng(pois.get(pois.size() - 1).getLocation())).mode(travelMode).waypoints(waypoints).await();
    return new Pair(nextPois, routes == null || routes.length == 0 ? null : routes[0]);
}
Example 7
Project: geo-platform-master  File: GPDirectionsServiceConfig.java View source code
@Override
public DirectionsApiRequest newRequest() {
    return DirectionsApi.newRequest(geoApiContext);
}