У меня это работает. Возник ряд проблем, в том числе то, что у меня был HTML в моем файле db_connect.php.
Вот рабочий код php:
<?php
/*
require_once("db_connect.php");
*/
$db_hostname = 'xxxxxxxxx';
$db_database = 'xxxxxxxx'; //'Your database name'
$db_username = 'xxxxxxxxxx'; //'username';
$db_password = 'xxxxxxxxxx'; //'password';
$db_status = 'not initialised';
$db_server = mysqli_connect($db_hostname, $db_username, $db_password);
$db_status = "connected";
if (!$db_server){
die("Unable to connect to MySQL: " . mysqli_connect_error());
$db_status = "not connected";
}
mysqli_select_db($db_server, $db_database) or die("Couldn't find db");
$query = "SELECT * FROM locations WHERE ID = 1";
$result = mysqli_query($db_server, $query);
if(!$result) die('Query failed: ' . mysqli_error($db_server));
$str_result = mysqli_num_rows($result). "rows<br />";
while($row = mysqli_fetch_array($result)){
$lat = $row['lat'] ;
$lng = $row['lng'] ;
}
echo $lat . "," . $lng ?>
И в файле для построения местоположения точки:
<!DOCTYPE html>
<html>
<head>
<title>Location tracking Google map</title>
</head>
<body>
<h1>Location tracking Google Map</h1>
<!-- testing, next 3 lines can be removed -->
<h4>Testing variables (these latlngs have no effect on the map):</h4>
<p id="responsecontainer"></p>
<p id="responsecheck"></p>
<!-- the map -->
<div id="map" style="width:600px;height:400px">My map will go here</div>
</body>
</html>
<!-- the scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
//Global variables for map and marker
var map;
var marker;
//function to set uo the map
function myMap() {
var mapOptions = {
center: new google.maps.LatLng(51.5, -0.12),
zoom: 10,
mapTypeId: google.maps.MapTypeId.HYBRID
}
map = new google.maps.Map(document.getElementById("map"), mapOptions);
marker = new google.maps.Marker({
position: new google.maps.LatLng(51.5, -0.12),
map: map,
title: 'Initial marker!'
});
}
//jQuery and ajax call to get new location location
$(document).ready(function() {
$(function retrieveData(){
$.ajax({ //create an ajax request to display.php
type: "GET",
url: "retrievedata.php",
dataType: "html", //expect html to be returned
success: function(response){
var res = response.split(",");
myLat = res[0];
myLng = res[1];
point = new google.maps.LatLng(myLat, myLng);
map.setCenter(point);
changeMarkerPosition(marker, point);
//testing - next two lines can be removed
$("#responsecontainer").html(response);
$("#responsecheck").html("myLat: " + myLat + ", myLng: " + myLng);
}
});
timerId = setTimeout(retrieveData, 1000);
});
});
//function to update marker location on map
function changeMarkerPosition(mymarker, location){
mymarker.setPosition(location);
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIsCXBsss2UeKrP80RfU-webxxV757b3j9vubcc&callback=myMap"></script>