I have this piece of code wich asks origin city and destination city as user input, it makes autocomplete and calculates time and km distance between 2 points , I would like to change it as instead of only accept city it could accept adresses and places to (with autocomplete). note that in order to use it you need to provide your own google api.
here is the snippet https://codepen.io/luiscunha96/pen/GRKNYpw
var myLatLng = { lat: 38.736946, lng: -9.142685 };
var mapOptions = {
center: myLatLng,
zoom: 1,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// Hide result box
document.getElementById("output").style.display = "none";
// Create/Init map
var map = new google.maps.Map(document.getElementById('google-map'), mapOptions);
// Create a DirectionsService object to use the route method and get a result for our request
var directionsService = new google.maps.DirectionsService();
// Create a DirectionsRenderer object which we will use to display the route
var directionsDisplay = new google.maps.DirectionsRenderer();
// Bind the DirectionsRenderer to the map
directionsDisplay.setMap(map);
// Define calcRoute function
function calcRoute() {
//create request
var request = {
origin: document.getElementById("location-1").value,
destination: document.getElementById("location-2").value,
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC
}
// Routing
directionsService.route(request, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
//Get distance and time
$("#output").html("<div class='result-table'> Driving distance: " + result.routes[0].legs[0].distance.text + ".<br />Duration: " + result.routes[0].legs[0].duration.text + ".</div>");
document.getElementById("output").style.display = "block";
//display route
directionsDisplay.setDirections(result);
} else {
//delete route from map
directionsDisplay.setDirections({ routes: [] });
//center map in London
map.setCenter(myLatLng);
//Show error message
alert("Can't find road! Please try again!");
clearRoute();
}
});
}
// Clear results
function clearRoute(){
document.getElementById("output").style.display = "none";
document.getElementById("location-1").value = "";
document.getElementById("location-2").value = "";
directionsDisplay.setDirections({ routes: [] });
}
// Create autocomplete objects for all inputs
var options = {
types: ['(cities)']
//types: ['(regions)']
//types: ['postal-code']
}
var input1 = document.getElementById("location-1");
var autocomplete1 = new google.maps.places.Autocomplete(input1, options);
var input2 = document.getElementById("location-2");
var autocomplete2 = new google.maps.places.Autocomplete(input2, options);