Page Summary
-
The Roads API Inspector is an interactive tool for visualizing and testing Google's Roads API, allowing users to input Roads API request URLs or utilize pre-loaded examples to see results on a map.
-
Users can explore functionalities like snapping points to roads, retrieving place details and speed limits, visualizing results with markers and polylines, and toggling interpolation and distance displays.
-
The tool provides detailed information about snapped points through customizable infowindows and highlights potential snapping issues by color-coding distances between original and snapped points.
-
The provided JavaScript code demonstrates how to interact with the Roads, Places, and Speed Limits APIs, showcasing functionalities such as snapping coordinates, retrieving place details, displaying results on a map, and handling errors.
The Roads API Inspector is an interactive tool that you can use to try out the Roads API. Here are some suggestions to get started with the tool:
- Copy a Roads API request URL into the text field and select Plot a Course, to explore the results of a request. You don't need to include an API key.
- Use the featured examples to experiment with the API.
- Select a marker for a particular point, to see an info window with details about the point.
- Open Street View after loading one of the examples to see the markers in the Street View panorama.
- Toggle the Interpolate setting to see its effect on the results.
- Select Toggle Distances to view or hide the distance between two original points. The unsnapped route is shown as a straight green line from point to point. Select the line to see the distance.
The featured examples are:
- Example 1: Points along a road in Pyrmont, Sydney.
- Example 2: Points along a road in Canberra, Australian Capital Territory.
- Example 3: Points along a road in Canberra with a point that can't be snapped to a road.
- Example 4: A route in Elkin, NC, with an erratic path that nicely demonstrates the results of toggling the interpolation setting.
View this example full screen.
Try it yourself
JavaScript
// Replace with your own API key var API_KEY = 'YOUR_API_KEY'; // Icons for markers var RED_MARKER = 'https://maps.google.com/mapfiles/ms/icons/red-dot.png'; var GREEN_MARKER = 'https://maps.google.com/mapfiles/ms/icons/green-dot.png'; var BLUE_MARKER = 'https://maps.google.com/mapfiles/ms/icons/blue-dot.png'; var YELLOW_MARKER = 'https://maps.google.com/mapfiles/ms/icons/yellow-dot.png'; // URL for places requests var PLACES_URL = 'https://maps.googleapis.com/maps/api/place/details/json?' + 'key=' + API_KEY + '&placeid='; // URL for Speed limits var SPEED_LIMIT_URL = 'https://roads.googleapis.com/v1/speedLimits'; var coords; /** * Current Roads API threshold (subject to change without notice) * @const {number} */ var DISTANCE_THRESHOLD_HIGH = 300; var DISTANCE_THRESHOLD_LOW = 200; /** * @type Array<ExtendedLatLng> */ var originals = []; // the original input points, a list of ExtendedLatLng var interpolate = true; var map; var placesService; var originalCoordsLength; // Settingup Arrays var infoWindows = []; var markers = []; var placeIds = []; var polylines = []; var snappedCoordinates = []; var distPolylines = []; // Symbol that gets animated along the polyline var lineSymbol = { path: google.maps.SymbolPath.CIRCLE, scale: 8, strokeColor: '#005db5', strokeWidth: '#005db5' }; // Example 1 - Frolick around Sydney var eg1 = '-33.870315,151.196532|-33.869979,151.197355|' + '-33.870044,151.197712|-33.870358,151.198206|' + '-33.870595,151.198376|-33.870640,151.198398|' + '-33.870620,151.198449|-33.870951,151.198525|' + '-33.871040,151.198528|-33.872031,151.198413'; // Example 2 - Lap around Canberra var eg2 = '-35.274346,149.130168|-35.278012,149.129583|' + '-35.280329,149.129073|-35.280999,149.129293|' + '-35.281441,149.129846|-35.281945,149.130034|' + '-35.282825,149.129567|-35.283022,149.128811|' + '-35.284734,149.128366'; // Example 3 - Path with unsnappable point var eg3 = '-35.274346,149.094000|-35.278012,149.129583|' + '-35.280329,149.129073|-35.280999,149.129293|' + '-35.281441,149.129846'; // Example 4 - Drive erratically in Elkin var eg4 = '36.28881,-80.8525|36.287038,-80.85313|36.286161,-80.85369|' + '36.28654,-80.85418|36.2846,-80.84766|36.28355,-80.84669'; // Initialize function initialize() { $('#eg1').click(function(e) { $('#coords').val(eg1); $('#plot').trigger('click'); }); $('#eg2').click(function(e) { $('#coords').val(eg2); $('#plot').trigger('click'); }); $('#eg3').click(function(e) { $('#coords').val(eg3); $('#plot').trigger('click'); }); $('#eg4').click(function(e) { $('#coords').val(eg4); $('#plot').trigger('click'); }); $('#toggle').click(function(e) { if ($('#panel').css("display") != 'none') { $('#toggle').html("+"); $('#panel').hide(); } else { $('#toggle').html("—"); $('#panel').show(); } }); // Centre the map on Sydney var mapOptions = { center: {'lat': -33.870315, 'lng': 151.196532}, zoom: 14 }; // Map object map = new google.maps.Map(document.getElementById('map'), mapOptions); // Places object placesService = new google.maps.places.PlacesService(map); // Reset the map to a clean state and reset all variables // used for displaying each request function clearMap() { // Clear the polyline for (var i = 0; i < polylines.length; i++) { polylines[i].setMap(null); } // Clear all markers for (var i = 0; i < markers.length; i++) { markers[i].setMap(null); } // Clear all the distance polylines for (var i = 0; i < distPolylines.length; i++) { distPolylines[i].setMap(null); } // Clear all info windows for (var i = 0; i < infoWindows.length; i++) { infoWindows[i].close(); } // Empty everything polylines = []; markers = []; distPolylines = []; snappedCoordinates = []; placeIds = []; infoWindows = []; $('#unsnappedPoints').empty(); $('#warningMessage').empty(); } // Parse the value in the input element // to get all coordinates function parseCoordsFromQuery(input) { var coords; input = decodeURIComponent(input); if (input.split('path=').length > 1) { input = decodeURIComponent(input); // Split on the ampersand to get all params var parts = input.split('&'); // Check each part to see if it starts with 'path=' // grabbing out the coordinates if it does for (var i = 0; i < parts.length; i++) { if (parts[i].split('path=').length > 1) { coords = parts[i].split('path=')[1]; break; } } } else { coords = decodeURIComponent(input); } // Parse the "Lat,Lng|..." coordinates into an array of ExtendedLatLng originals = []; var points = coords.split('|'); for (var i = 0; i < points.length; i++) { var point = points[i].split(','); originals.push({lat: Number(point[0]), lng: Number(point[1]), index:i}); } return coords; } // Clear the map of any old data and plot the request $('#plot').click(function(e) { clearMap(); bendAndSnap(); drawDistance(); e.preventDefault(); }); // Make AJAX request to the snapToRoadsAPI // with coordinates parsed from text input element. function bendAndSnap() { coords = parseCoordsFromQuery(