What potential issues can arise when passing PHP variables to JavaScript in Google Maps API integration?

One potential issue that can arise when passing PHP variables to JavaScript in Google Maps API integration is that the PHP variables may not be properly escaped or formatted for JavaScript, leading to errors or unexpected behavior. To solve this, you can use the json_encode function in PHP to convert the PHP variables into a JSON format that can be easily passed to JavaScript.

<?php
// Define PHP variables
$latitude = 37.7749;
$longitude = -122.4194;

// Encode PHP variables into JSON format
$data = array(
    'latitude' => $latitude,
    'longitude' => $longitude
);
$json_data = json_encode($data);
?>

<script>
// Pass encoded PHP variables to JavaScript
var mapData = <?php echo $json_data; ?>;
console.log(mapData);
</script>