I use google map with api places . Fill input origin then destination and set direction between two points. My problem is in every fill input I got this message . Any idea please to resolve this problem . How I can set marker of every point after search , then trace route between two markers enter image description here
<div id="google-map"></div>
<div class="row">
<div class="fieldlabels">
<label>Origin: </label>
</div>
<div class="location-input">
<input type="text" id="location1" name="origin" placeholder="Enter a start location..." required />
</div>
</div>
<!-- Location 2 -->
<div class="row">
<div class="fieldlabels">
<label>Destination: </label>
</div>
<div class="location-input">
<input type="text" id="location2" name="destination" placeholder="Enter a last location..." required />
</div>
</div>
<script>
function initMap() {
const directionsRenderer = new google.maps.DirectionsRenderer();
const directionsService = new google.maps.DirectionsService();
const map1 = new google.maps.Map(document.getElementById("google-map"), {
zoom: 7,
center: { lat: 48.85661, lng: 2.35222 },
disableDefaultUI: true,
});
directionsRenderer.setMap(map1);
directionsRenderer.setPanel(document.getElementById("sidebar"));
const control = document.getElementById("floating-panel");
map1.controls[google.maps.ControlPosition.TOP_CENTER].push(control);
const onChangeHandler = function () {
calculateAndDisplayRoute(directionsService, directionsRenderer);
};
document.getElementById("location1").addEventListener("change", onChangeHandler);
document.getElementById("location2").addEventListener("change", onChangeHandler);
originautocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */
(document.getElementById("location1")),
{
types: ["geocode"],
}
);
destinationautocomplete = new google.maps.places.Autocomplete(document.getElementById("location2"), {
types: ["geocode"],
});
}
function calculateAndDisplayRoute(directionsService, directionsRenderer, status) {
const start = document.getElementById("location1").value;
const end = document.getElementById("location2").value;
directionsService
.route({
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING,
})
.then((response) => {
console.log(response);
directionsRenderer.setDirections(response);
})
.catch((e) => window.alert("Directions request failed due to " + status));
}
window.initMap = initMap();
</script>