What is the best practice for centering a Google Maps marker based on data retrieved from a MySQL database using PHP?

When centering a Google Maps marker based on data retrieved from a MySQL database using PHP, the best practice is to calculate the average latitude and longitude values of the markers and use those values to set the center of the map. This ensures that the map is centered around all the markers.

<?php
// Connect to MySQL database
$connection = mysqli_connect('localhost', 'username', 'password', 'database');

// Retrieve marker data from database
$query = "SELECT * FROM markers";
$result = mysqli_query($connection, $query);

// Calculate average latitude and longitude
$latSum = 0;
$lngSum = 0;
$count = 0;

while ($row = mysqli_fetch_assoc($result)) {
    $latSum += $row['latitude'];
    $lngSum += $row['longitude'];
    $count++;
}

$avgLat = $latSum / $count;
$avgLng = $lngSum / $count;

// Output JavaScript to center map
echo "<script>
    var map;
    function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
            center: {lat: $avgLat, lng: $avgLng},
            zoom: 10
        });
    }
</script>";
?>