0

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>
2
  • 1
    You can echo it as js variable: echo "<script>var lat1 = ".$lat1.";</script>"; Commented Nov 28, 2017 at 10:20
  • Are the php and js code in the same file in your project? or in separate files? Commented Nov 28, 2017 at 10:23

2 Answers 2

4

Just print it in your javascript code with php like below:

<?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: <?php echo $lat1 ?>, lng: <?php echo $lon1 ?>}; //Send $point1 value here
        var point2 = {lat: <?php echo $lat2 ?>, lng: <?php echo $lon2 ?>}; //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);
      }
    });

Sign up to request clarification or add additional context in comments.

Comments

2

You can just echo the data into the JS variables like this...

var point1 = {lat: <?php echo $lat1 ?>, lng: <?php echo $lon1 ?>}; 
var point2 = {lat: <?php echo $lat2 ?>, lng: <?php echo $lon2 ?>};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.