标记未显示在谷歌地图上,但在控制台中没有错误
2022-08-30 20:02:28
一段时间以来,我一直在研究这个问题:尝试使用json从我的数据库中获取lat / lng值,然后使用这些值将标记输出到Google地图上。目前,地图显示时没有标记,并且控制台中没有错误。任何帮助将不胜感激!
<script type="text/javascript">
var map = null;
function addMarker(lat,lng) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
map: map,
});
}
function initialize() {
var mapOptions = {
center: {lat: 54.872128, lng: - 6.284874},
zoom: 13
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
$(document).ready(function(){
$.getJSON('MarkersController.php', function(data){
var locations = JSON.parse(data);
for(var i =0; i<locations.length; i++){
addMarker(locations['lat'], locations['lng']);
}
});
});
}
google.maps.event.addDomListener(window, 'load', initialize);
用于从 mysql 检索的 php 脚本:
<?php
include('DB_INFO.php');
// Opens a connection to a MySQL server.
$connection = mysqli_connect($server, $username, $password);
if (!$connection) {
die('Not connected : ' . mysqli_error());
}
// Sets the active MySQL database.
$db_selected = mysqli_select_db($database, $connection);
if (!$db_selected) {
die('Can\'t use db : ' . mysqli_error());
}
// Selects all the rows in the markers table.
$query = 'SELECT * FROM tester';
$result = mysqli_query($query);
if (!$result) {
die('Invalid query: '. mysqli_error());
}
$list = array();
while ($row = mysqli_fetch_assoc($result)) {
$list[] = $row;
}
header('Content-Type: application/json');
echo json_encode($list);
?>