com.google.maps.model.TravelMode Java Examples

The following examples show how to use com.google.maps.model.TravelMode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ArmeriaRequestHandler.java    From curiostack with MIT License 6 votes vote down vote up
private static Gson gsonForPolicy(FieldNamingPolicy fieldNamingPolicy) {
  return GSONS.computeIfAbsent(
      fieldNamingPolicy,
      policy ->
          new GsonBuilder()
              .registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeAdapter())
              .registerTypeAdapter(Distance.class, new DistanceAdapter())
              .registerTypeAdapter(Duration.class, new DurationAdapter())
              .registerTypeAdapter(Fare.class, new FareAdapter())
              .registerTypeAdapter(LatLng.class, new LatLngAdapter())
              .registerTypeAdapter(
                  AddressComponentType.class, new SafeEnumAdapter<>(AddressComponentType.UNKNOWN))
              .registerTypeAdapter(AddressType.class, new SafeEnumAdapter<>(AddressType.UNKNOWN))
              .registerTypeAdapter(TravelMode.class, new SafeEnumAdapter<>(TravelMode.UNKNOWN))
              .registerTypeAdapter(
                  LocationType.class, new SafeEnumAdapter<>(LocationType.UNKNOWN))
              .registerTypeAdapter(RatingType.class, new SafeEnumAdapter<>(RatingType.UNKNOWN))
              .registerTypeAdapter(DayOfWeek.class, new DayOfWeekAdapter())
              .registerTypeAdapter(PriceLevel.class, new PriceLevelAdapter())
              .registerTypeAdapter(Instant.class, new InstantAdapter())
              .registerTypeAdapter(LocalTime.class, new LocalTimeAdapter())
              .registerTypeAdapter(
                  GeolocationApi.Response.class, new GeolocationResponseAdapter())
              .setFieldNamingPolicy(policy)
              .create());
}
 
Example #2
Source File: DirectionsApiTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testTravelModeWalking() throws Exception {
  try (LocalTestServerContext sc =
      new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) {
    DirectionsResult result =
        DirectionsApi.newRequest(sc.context)
            .mode(TravelMode.WALKING)
            .origin("483 George St, Sydney NSW 2000, Australia")
            .destination("182 Church St, Parramatta NSW 2150, Australia")
            .await();

    assertNotNull(result.toString());
    assertNotNull(result.routes);
    assertNotNull(result.routes[0]);

    sc.assertParamValue(TravelMode.WALKING.toUrlValue(), "mode");
    sc.assertParamValue("483 George St, Sydney NSW 2000, Australia", "origin");
    sc.assertParamValue("182 Church St, Parramatta NSW 2150, Australia", "destination");

    assertNotNull(result.toString());
  }
}
 
Example #3
Source File: DirectionsApiTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
/** Test the extended transit parameters: mode and routing preference. */
@Test
public void testTransitParams() throws Exception {
  try (LocalTestServerContext sc =
      new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) {
    DirectionsResult result =
        DirectionsApi.newRequest(sc.context)
            .origin("Fisherman's Wharf, San Francisco")
            .destination("Union Square, San Francisco")
            .mode(TravelMode.TRANSIT)
            .transitMode(TransitMode.BUS, TransitMode.TRAM)
            .transitRoutingPreference(TransitRoutingPreference.LESS_WALKING)
            .await();

    sc.assertParamValue("Fisherman's Wharf, San Francisco", "origin");
    sc.assertParamValue("Union Square, San Francisco", "destination");
    sc.assertParamValue(TravelMode.TRANSIT.toUrlValue(), "mode");
    sc.assertParamValue(
        TransitMode.BUS.toUrlValue() + "|" + TransitMode.TRAM.toUrlValue(), "transit_mode");
    sc.assertParamValue(
        TransitRoutingPreference.LESS_WALKING.toUrlValue(), "transit_routing_preference");

    assertNotNull(result.toString());
  }
}
 
Example #4
Source File: DirectionsApiTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
/** Test transit without arrival or departure times specified. */
@Test
public void testTransitWithoutSpecifyingTime() throws Exception {
  try (LocalTestServerContext sc =
      new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) {
    DirectionsResult result =
        DirectionsApi.newRequest(sc.context)
            .origin("Fisherman's Wharf, San Francisco")
            .destination("Union Square, San Francisco")
            .mode(TravelMode.TRANSIT)
            .await();

    sc.assertParamValue("Fisherman's Wharf, San Francisco", "origin");
    sc.assertParamValue("Union Square, San Francisco", "destination");
    sc.assertParamValue(TravelMode.TRANSIT.toUrlValue(), "mode");

    assertNotNull(result.toString());
  }
}
 
Example #5
Source File: DirectionsApiTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
/** Tests the {@code traffic_model} and {@code duration_in_traffic} parameters. */
@Test
public void testTrafficModel() throws Exception {
  try (LocalTestServerContext sc =
      new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) {
    DirectionsResult result =
        DirectionsApi.newRequest(sc.context)
            .origin("48 Pirrama Road, Pyrmont NSW 2009")
            .destination("182 Church St, Parramatta NSW 2150")
            .mode(TravelMode.DRIVING)
            .departureTime(Instant.now().plus(Duration.ofMinutes(2)))
            .trafficModel(TrafficModel.PESSIMISTIC)
            .await();

    sc.assertParamValue("48 Pirrama Road, Pyrmont NSW 2009", "origin");
    sc.assertParamValue("182 Church St, Parramatta NSW 2150", "destination");
    sc.assertParamValue(TravelMode.DRIVING.toUrlValue(), "mode");
    sc.assertParamValue(TrafficModel.PESSIMISTIC.toUrlValue(), "traffic_model");

    assertNotNull(result.toString());
  }
}
 
Example #6
Source File: DirectionsApiTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
/**
 * Brooklyn to Queens by public transport.
 *
 * <p>{@code
 * http://maps.googleapis.com/maps/api/directions/json?origin=Brooklyn&destination=Queens&departure_time=1343641500&mode=transit}
 */
@Test
public void testBrooklynToQueensByTransit() throws Exception {
  try (LocalTestServerContext sc =
      new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) {
    DirectionsApi.newRequest(sc.context)
        .origin("Brooklyn")
        .destination("Queens")
        .mode(TravelMode.TRANSIT)
        .await();

    sc.assertParamValue("Brooklyn", "origin");
    sc.assertParamValue("Queens", "destination");
    sc.assertParamValue(TravelMode.TRANSIT.toUrlValue(), "mode");
  }
}
 
Example #7
Source File: DirectionsApiTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
/**
 * Going from Toronto to Montreal by bicycle, avoiding highways.
 *
 * <p>{@code
 * http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&avoid=highways&mode=bicycling}
 */
@Test
public void testTorontoToMontrealByBicycleAvoidingHighways() throws Exception {
  try (LocalTestServerContext sc =
      new LocalTestServerContext("{\"routes\": [{}],\"status\": \"OK\"}")) {
    DirectionsApi.newRequest(sc.context)
        .origin("Toronto")
        .destination("Montreal")
        .avoid(DirectionsApi.RouteRestriction.HIGHWAYS)
        .mode(TravelMode.BICYCLING)
        .await();

    sc.assertParamValue("Toronto", "origin");
    sc.assertParamValue("Montreal", "destination");
    sc.assertParamValue(RouteRestriction.HIGHWAYS.toUrlValue(), "avoid");
    sc.assertParamValue(TravelMode.BICYCLING.toUrlValue(), "mode");
  }
}
 
Example #8
Source File: DirectionsApiTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testResponseTimesArePopulatedCorrectly() throws Exception {
  try (LocalTestServerContext sc =
      new LocalTestServerContext(responseTimesArePopulatedCorrectly)) {
    DirectionsResult result =
        DirectionsApi.newRequest(sc.context)
            .mode(TravelMode.TRANSIT)
            .origin("483 George St, Sydney NSW 2000, Australia")
            .destination("182 Church St, Parramatta NSW 2150, Australia")
            .await();

    assertEquals(1, result.routes.length);
    assertEquals(1, result.routes[0].legs.length);
    DateTimeFormatter fmt = DateTimeFormatter.ofPattern("h:mm a");
    assertEquals("1:54 pm", fmt.format(result.routes[0].legs[0].arrivalTime).toLowerCase());
    assertEquals("1:21 pm", fmt.format(result.routes[0].legs[0].departureTime).toLowerCase());

    sc.assertParamValue(TravelMode.TRANSIT.toUrlValue(), "mode");
    sc.assertParamValue("483 George St, Sydney NSW 2000, Australia", "origin");
    sc.assertParamValue("182 Church St, Parramatta NSW 2150, Australia", "destination");
  }
}
 
Example #9
Source File: DistanceMatrixApiTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
/** Test duration in traffic with traffic model set. */
@Test
public void testDurationInTrafficWithTrafficModel() throws Exception {
  try (LocalTestServerContext sc = new LocalTestServerContext("{\"status\" : \"OK\"}")) {
    final long ONE_HOUR_MILLIS = 60 * 60 * 1000;
    DistanceMatrixApi.newRequest(sc.context)
        .origins("Fisherman's Wharf, San Francisco")
        .destinations("San Francisco International Airport, San Francisco, CA")
        .mode(TravelMode.DRIVING)
        .trafficModel(TrafficModel.PESSIMISTIC)
        .departureTime(Instant.ofEpochMilli(System.currentTimeMillis() + ONE_HOUR_MILLIS))
        .await();

    sc.assertParamValue("Fisherman's Wharf, San Francisco", "origins");
    sc.assertParamValue("San Francisco International Airport, San Francisco, CA", "destinations");
    sc.assertParamValue(TravelMode.DRIVING.toUrlValue(), "mode");
    sc.assertParamValue(TrafficModel.PESSIMISTIC.toUrlValue(), "traffic_model");
  }
}
 
Example #10
Source File: DistanceMatrixApiTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
/** Test transit without arrival or departure times specified. */
@Test
public void testTransitWithoutSpecifyingTime() throws Exception {
  try (LocalTestServerContext sc = new LocalTestServerContext("{\"status\" : \"OK\"}")) {
    String[] origins =
        new String[] {"Fisherman's Wharf, San Francisco", "Union Square, San Francisco"};
    String[] destinations =
        new String[] {"Mikkeller Bar, San Francisco", "Moscone Center, San Francisco"};
    DistanceMatrixApi.newRequest(sc.context)
        .origins(origins)
        .destinations(destinations)
        .mode(TravelMode.TRANSIT)
        .await();

    sc.assertParamValue(StringUtils.join(origins, "|"), "origins");
    sc.assertParamValue(StringUtils.join(destinations, "|"), "destinations");
    sc.assertParamValue(TravelMode.TRANSIT.toUrlValue(), "mode");
  }
}
 
Example #11
Source File: DistanceMatrixApiTest.java    From google-maps-services-java with Apache License 2.0 6 votes vote down vote up
/**
 * Test the language parameter.
 *
 * <p>Sample request: <a
 * href="http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR">
 * origins: Vancouver BC|Seattle, destinations: San Francisco|Victoria BC, mode: bicycling,
 * language: french</a>.
 */
@Test
public void testLanguageParameter() throws Exception {
  try (LocalTestServerContext sc = new LocalTestServerContext("{\"status\" : \"OK\"}")) {
    String[] origins = new String[] {"Vancouver BC", "Seattle"};
    String[] destinations = new String[] {"San Francisco", "Victoria BC"};
    DistanceMatrixApi.newRequest(sc.context)
        .origins(origins)
        .destinations(destinations)
        .mode(TravelMode.BICYCLING)
        .language("fr-FR")
        .await();

    sc.assertParamValue(StringUtils.join(origins, "|"), "origins");
    sc.assertParamValue(StringUtils.join(destinations, "|"), "destinations");
    sc.assertParamValue(TravelMode.BICYCLING.toUrlValue(), "mode");
    sc.assertParamValue("fr-FR", "language");
  }
}
 
Example #12
Source File: DirectionsApiTest.java    From google-maps-services-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuilder() throws Exception {
  try (LocalTestServerContext sc = new LocalTestServerContext(builderResponse)) {
    DirectionsResult result =
        DirectionsApi.newRequest(sc.context)
            .mode(TravelMode.BICYCLING)
            .avoid(
                DirectionsApi.RouteRestriction.HIGHWAYS,
                DirectionsApi.RouteRestriction.TOLLS,
                DirectionsApi.RouteRestriction.FERRIES)
            .units(Unit.METRIC)
            .region("au")
            .origin("Sydney")
            .destination("Melbourne")
            .await();

    assertNotNull(result.routes);
    assertEquals(1, result.routes.length);

    sc.assertParamValue(TravelMode.BICYCLING.toUrlValue(), "mode");
    sc.assertParamValue(
        DirectionsApi.RouteRestriction.HIGHWAYS.toUrlValue()
            + "|"
            + DirectionsApi.RouteRestriction.TOLLS.toUrlValue()
            + "|"
            + DirectionsApi.RouteRestriction.FERRIES.toUrlValue(),
        "avoid");
    sc.assertParamValue(Unit.METRIC.toUrlValue(), "units");
    sc.assertParamValue("au", "region");
    sc.assertParamValue("Sydney", "origin");
    sc.assertParamValue("Melbourne", "destination");
  }
}
 
Example #13
Source File: DistanceMatrixApiTest.java    From google-maps-services-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewRequestWithAllPossibleParams() throws Exception {
  try (LocalTestServerContext sc = new LocalTestServerContext("{\"status\" : \"OK\"}")) {
    String[] origins =
        new String[] {
          "Perth, Australia", "Sydney, Australia", "Melbourne, Australia",
          "Adelaide, Australia", "Brisbane, Australia", "Darwin, Australia",
          "Hobart, Australia", "Canberra, Australia"
        };
    String[] destinations =
        new String[] {
          "Uluru, Australia",
          "Kakadu, Australia",
          "Blue Mountains, Australia",
          "Bungle Bungles, Australia",
          "The Pinnacles, Australia"
        };

    DistanceMatrixApi.newRequest(sc.context)
        .origins(origins)
        .destinations(destinations)
        .mode(TravelMode.DRIVING)
        .language("en-AU")
        .avoid(RouteRestriction.TOLLS)
        .units(Unit.IMPERIAL)
        .departureTime(
            Instant.now().plus(Duration.ofMinutes(2))) // this is ignored when an API key is used
        .await();

    sc.assertParamValue(StringUtils.join(origins, "|"), "origins");
    sc.assertParamValue(StringUtils.join(destinations, "|"), "destinations");
    sc.assertParamValue(TravelMode.DRIVING.toUrlValue(), "mode");
    sc.assertParamValue("en-AU", "language");
    sc.assertParamValue(RouteRestriction.TOLLS.toUrlValue(), "avoid");
    sc.assertParamValue(Unit.IMPERIAL.toUrlValue(), "units");
  }
}
 
Example #14
Source File: DirectionsApiTest.java    From google-maps-services-java with Apache License 2.0 5 votes vote down vote up
/** Test GeocodedWaypoints results. */
@Test
public void testGeocodedWaypoints() throws Exception {
  try (LocalTestServerContext sc =
      new LocalTestServerContext(
          "{"
              + "   \"geocoded_waypoints\" : [\n"
              + "      {\n"
              + "         \"geocoder_status\" : \"OK\"\n"
              + "      },\n"
              + "      {\n"
              + "         \"geocoder_status\" : \"OK\",\n"
              + "         \"types\" : [\"route\"]\n"
              + "      }\n"
              + "   ],\n"
              + "   \"routes\": [{}],\n"
              + "   \"status\": \"OK\"\n"
              + "}")) {
    DirectionsResult result =
        DirectionsApi.newRequest(sc.context)
            .origin("48 Pirrama Rd, Pyrmont NSW")
            .destination("Airport Dr, Sydney NSW")
            .mode(TravelMode.DRIVING)
            .await();

    assertNotNull(result.toString());
    assertNotNull(result.geocodedWaypoints);
    assertEquals(2, result.geocodedWaypoints.length);
    assertEquals(GeocodedWaypointStatus.OK, result.geocodedWaypoints[0].geocoderStatus);
    assertEquals(GeocodedWaypointStatus.OK, result.geocodedWaypoints[1].geocoderStatus);
    assertEquals(AddressType.ROUTE, result.geocodedWaypoints[1].types[0]);

    assertNotNull(result.toString());
  }
}
 
Example #15
Source File: RecommendedRouteFragment.java    From rox-android with Apache License 2.0 5 votes vote down vote up
private void recalculateRoute(TravelMode travelMode) {
    saveCurrentTravelMode(travelMode);
    recalculateRouteTask = new RecalculateRouteTask()
            .origin(getCurrentLocationArg())
            .pois(pois)
            .travelMode(travelMode);
    recalculateRouteTask.request();
}
 
Example #16
Source File: DirectionsWorkitemHandler.java    From jbpm-work-items with Apache License 2.0 4 votes vote down vote up
public void executeWorkItem(WorkItem workItem,
                            WorkItemManager workItemManager) {

    String origin = (String) workItem.getParameter("Origin");
    String destination = (String) workItem.getParameter("Destination");
    String mode = (String) workItem.getParameter("Mode");
    String avoid = (String) workItem.getParameter("Avoid");
    String units = (String) workItem.getParameter("Units");

    try {

        RequiredParameterValidator.validate(this.getClass(),
                                            workItem);

        Map<String, Object> results = new HashMap<>();

        DirectionsApiRequest directionsApiRequest = DirectionsApi.newRequest(geoApiContext);
        directionsApiRequest = directionsApiRequest.origin(origin);
        directionsApiRequest = directionsApiRequest.destination(destination);

        if (mode != null) {
            switch (mode) {
                case "driving":
                    directionsApiRequest = directionsApiRequest.mode(TravelMode.DRIVING);
                    break;
                case "walking":
                    directionsApiRequest = directionsApiRequest.mode(TravelMode.WALKING);
                    break;
                case "bicycling":
                    directionsApiRequest = directionsApiRequest.mode(TravelMode.BICYCLING);
                    break;
                case "transit":
                    directionsApiRequest = directionsApiRequest.mode(TravelMode.TRANSIT);
                    break;
                default:
                    directionsApiRequest = directionsApiRequest.mode(TravelMode.DRIVING);
            }
        }

        if (avoid != null) {
            switch (avoid) {
                case "tolls":
                    directionsApiRequest = directionsApiRequest.avoid(DirectionsApi.RouteRestriction.TOLLS);
                    break;
                case "highways":
                    directionsApiRequest = directionsApiRequest.avoid(DirectionsApi.RouteRestriction.HIGHWAYS);
                    break;
                case "ferries":
                    directionsApiRequest = directionsApiRequest.avoid(DirectionsApi.RouteRestriction.FERRIES);
                    break;
                default:
            }
        }

        if (units != null) {
            switch (units) {
                case "metric":
                    directionsApiRequest = directionsApiRequest.units(Unit.METRIC);
                    break;
                case "imperial":
                    directionsApiRequest = directionsApiRequest.units(Unit.IMPERIAL);
                    break;
                default:
                    directionsApiRequest = directionsApiRequest.units(Unit.METRIC);
            }
        }

        DirectionsResult directionsResult = directionsApiRequest.await();
        results.put(RESULTS_VALUE,
                    directionsResult);

        workItemManager.completeWorkItem(workItem.getId(),
                                         results);
    } catch (Exception e) {
        handleException(e);
    }
}
 
Example #17
Source File: RecommendedRouteFragment.java    From rox-android with Apache License 2.0 4 votes vote down vote up
private TravelMode getCurrentTravelMode() {
    String travelModeStr = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext())
            .getString(getString(R.string.travel_mode_key), TravelMode.DRIVING.name());
    return TravelMode.valueOf(travelModeStr);
}
 
Example #18
Source File: RecommendedRouteFragment.java    From rox-android with Apache License 2.0 4 votes vote down vote up
private void saveCurrentTravelMode(TravelMode selectedTravelMode) {
    PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext())
            .edit()
            .putString(getString(R.string.travel_mode_key), selectedTravelMode.name())
            .commit();
}