gpt4 book ai didi

javascript - 标记未显示在谷歌地图上,但控制台中没有错误

转载 作者:IT王子 更新时间:2023-10-28 23:53:09 25 4
gpt4 key购买 nike

我已经为此纠结了一段时间:尝试使用 json 从我的数据库中获取 lat/lng 值,然后使用这些值将标记输出到 google map 上。目前 map 显示时没有标记,控制台中也没有错误。任何帮助将不胜感激!

 <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);
?>

最佳答案

如果你有一个标记集合,我认为你应该使用这种方式迭代集合并分配 i 元素:

for(var i =0; i<locations.length; i++){
addMarker(locations[i].lat, locations[i].lng);
}

proof of concept fiddle

关于javascript - 标记未显示在谷歌地图上,但控制台中没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32071318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com