I am not a native AJAX and javascript programmer. I am trying to pass the longitude and latitude values from MySQL to javascript. However, i don't seem to have much luck here. Can anyone advise me what i am doing wrongly?
I am trying to create a script that allows me to create a javascript variable by using AJAX to call the values from the GPS_tracker php file.
GPS_tracker.php:
<?php
$servername = "localhost";
$username = "db_001";
$password = "12345678";
$dbname = "gpstable";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT rider_id, track_time, track_lng, track_lat FROM db_001.gpstable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$lat = $row['track_lat'];
$lng = $row['track_lng'];
$gpsjson = json_encode(array($lat, $lng));
}
} else {
echo "0 results";
}
$conn->close();
?>
Script.js:
$.ajax({
url: 'gps_tracker.php',
dataType: 'json'
}).done(
function(gpsjson){
var tag_name = gpsjson[0];
var client_id = gpsjson[1];
}
);
// Show the user's position on a Google map.
function showMap(lat, lon) {
// Create a LatLng object with the GPS coordinates.
var myLatLng = new google.maps.LatLng(lat, lon);
// Create the Map Options
var mapOptions = {
zoom: 8,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// Generate the Map
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
// Add a Marker to the Map
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Found you!'
});
}