0

I'm trying to find a way to make a loop that will slap a bunch of markers onto a map I have on my website using Lat and Long data from a database I have connected up. Very new to AJAX and I don't understand it at all, what I have made is from examples I have found online. Would really prefer if the answer didn't contain JQuery.

What I have tried to do is create an AJAX request to open a query for my database. The database would then call for all the data under columns "ID, Lng and Lat" and save them into an array. The array is converted into JSON and echo'd so Page1 can see it and use it to make a loop to make all the markers. The database is already linked up so no this question is not already answered stack overflow, and it requires a bit more than just connected a database to the main website, which again, is already done. I'm just not showing the details to get into my database.

page1.phtml

// Make an AJAX request to get the marker data
                var xhr = new XMLHttpRequest();
                xhr.open("GET", "createNodes.php");
                xhr.onload = function() {
                  if (xhr.status === 200) {
                    // Parse the JSON data
                    var data = JSON.parse(xhr.responseText);
            
                    // Loop through the marker data and create a marker for each one
                    data.forEach(function($dataset) {
                      L.marker([marker.lat, marker.lng]).addTo(map).bindPopup(marker.name);
                    });
                  } else {
                    console.log("Request failed.  Returned status of " + xhr.status);
                  }
                };
                xhr.send();

createNodes.php


//Creates the connection with the above information
$dbhandle = new mysqli("mysql:host=$servername;dbname=$dbusername", $database, $dbpassword);

// Retrieve marker data from the database
$query = "SELECT ID, Lat, Lng FROM ChargePoint";
$result = mysqli_query($dbhandle, $query);
$statement = $dbhandle->prepare($query);
$statement->execute();

$dataset = ($statement->fetch());

echo json_encode($dataset);

However, when run the map has no markers on it at all. I have written and rewritten this multiple times based on what information I can find in other questions and online areas, so any help at all would be appreciated.

0 Answers0