I have a coordinate(latlng) in a php variable. I need to pass these variables to the javascript google maps api request. I want to pass $lat1 and $lon1 to point1 in javascript without onClick function. How can I achieve this ?
<?php
$lat1 = 6.893732;
$lon1 = 79.857516;
$lat2 = 6.856007;
$lon2 = 79.865284;
?>
<html>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"
async defer></script>
<script>
var point1 = {lat: 41.85, lng: -87.65}; //Send $point1 value here
var point2 = {lat: 39.79, lng: -86.14}; //Send $point2 value here
// Set destination, origin and travel mode.
var request = {
destination: point1,
origin: point2,
travelMode: 'DRIVING'
};
// Pass the directions request to the directions service.
var directionsService = new google.maps.DirectionsService();
directionsService.route(request, function(response, status) {
if (status == 'OK') {
// Display the route on the map.
directionsDisplay.setDirections(response);
}
});
</script>