Created
March 16, 2016 13:09
-
-
Save gelias/855b69dc0eecb13d906b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package cc.routing.gmaps.service | |
| import com.google.maps.DirectionsApi; | |
| import com.google.maps.DirectionsApiRequest; | |
| import com.google.maps.GeoApiContext; | |
| import com.google.maps.model.DirectionsRoute; | |
| import com.google.maps.model.LatLng; | |
| import com.google.maps.model.TravelMode; | |
| public class GMapsDirectionsService { | |
| private static final String API_KEY = "SET_API_KEY_HERE"; | |
| public static void main(String[] args) { | |
| GMapsDirectionsService service = new GMapsDirectionsService(); | |
| DirectionsRoute directions = service.getDirections(); | |
| System.out.println(directions.legs.length); | |
| } | |
| public DirectionsRoute getDirections() { | |
| try { | |
| GeoApiContext apiContext = new GeoApiContext(); | |
| apiContext.setApiKey(API_KEY); | |
| DirectionsApiRequest request = DirectionsApi.newRequest(apiContext); | |
| request.mode(TravelMode.DRIVING); | |
| request.origin(getUfrgsAsOrigin()); | |
| request.destination(getUmovmeAsDestination()); | |
| request.waypoints(getWaypoints()); | |
| return request.await()[0]; | |
| } catch (Exception e) { | |
| throw new RuntimeException("Error making route request",e); | |
| } | |
| } | |
| private String[] getWaypoints() { | |
| String mercadoPublicoPortoAlegre = "-30.0284528, -51.22809719999998"; | |
| String monumentoAoLacador = "-30.0346471, -51.217658400000005"; | |
| String[] waypoints = { mercadoPublicoPortoAlegre, monumentoAoLacador }; | |
| return waypoints; | |
| } | |
| private LatLng getUmovmeAsDestination() { | |
| return new LatLng(-30.0121269, -51.19688150000002); | |
| } | |
| private LatLng getUfrgsAsOrigin() { | |
| return new LatLng(-30.0322574, -51.22044390000002); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment