Javascript & PHP 在 Google 地图中显示多个追踪器 API
Javascript & PHP show multiple trackers in Google Maps API
目前,我正在开发一个系统,它将从 MySQLi DB 获取所有纬度和经度,并将输出页面上的所有数据。这是我的 PHP 代码(工作没有任何问题):
header('Content-Type: text/html; charset=utf-8');
require('database.php');
$query = mysqli_query($mysqli, "SELECT * FROM `tracking`");
if (!$query)
{
die('Error: ' . mysqli_error($mysqli));
}
if(mysqli_num_rows($query) > 0){
$getList = array();
while ($row = mysqli_fetch_assoc($query)) {
$getList[] = $row;
}
for($i = 0, $l = count($getList); $i < $l; ++$i) {
echo $getList[$i]['lat'].",".$getList[$i]['lon']."\n";
}
}
例如输出:-71.99991299555996,-83.18116602 -22.809918399999997,-43.4211132 -22.8540416,-43.2488448
现在,我需要将所有数据放入一个数组中,并将整个 latitude/longitude 放入 Google 地图中的标记。检查我尝试了什么:
var check = false;
function refreshAll() { // This function will be called every 60 seconds
var locations = [];
locations.length = 0;
$.ajax({
type: "GET",
url: "show.php",
success: function(x)
{
var items = x.split("\n");
for(i=0; i<items.length; i++){
var data = items[i];
if ((items[i]).length > 1) {
locations.push(items[i].trim());
}
}
if (check == false) { // this will avoid map refreshing, I need only markers update.
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
check = true;
}
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
alert(locations[i]);
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i].trim()),
map: map
});
}
}
});}
问题是:未设置标记,控制台中没有错误。最近两天我正在处理这个问题,但找不到任何解决方案。你能帮助我吗?谢谢。
查看,地图对象在您的 infoWindow 变量中,尝试在那里设置位置。
这应该在位置循环内使用。
var pos = {
lat: latitude,
lng: longitude
};
infoWindow.setPosition(pos);
如您所见here in the docs,LatLng 对象与您使用的对象不同。
所以,替换:
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i].trim()),
map: map
});
有
locations[i] = locations[i].trim().split(',');
var latLng = {lat: Number(locations[i][0]), lng: Number(locations[i][1])};
marker = new google.maps.Marker({
position: latLng,
map: map
});
目前,我正在开发一个系统,它将从 MySQLi DB 获取所有纬度和经度,并将输出页面上的所有数据。这是我的 PHP 代码(工作没有任何问题):
header('Content-Type: text/html; charset=utf-8');
require('database.php');
$query = mysqli_query($mysqli, "SELECT * FROM `tracking`");
if (!$query)
{
die('Error: ' . mysqli_error($mysqli));
}
if(mysqli_num_rows($query) > 0){
$getList = array();
while ($row = mysqli_fetch_assoc($query)) {
$getList[] = $row;
}
for($i = 0, $l = count($getList); $i < $l; ++$i) {
echo $getList[$i]['lat'].",".$getList[$i]['lon']."\n";
}
}
例如输出:-71.99991299555996,-83.18116602 -22.809918399999997,-43.4211132 -22.8540416,-43.2488448
现在,我需要将所有数据放入一个数组中,并将整个 latitude/longitude 放入 Google 地图中的标记。检查我尝试了什么:
var check = false;
function refreshAll() { // This function will be called every 60 seconds
var locations = [];
locations.length = 0;
$.ajax({
type: "GET",
url: "show.php",
success: function(x)
{
var items = x.split("\n");
for(i=0; i<items.length; i++){
var data = items[i];
if ((items[i]).length > 1) {
locations.push(items[i].trim());
}
}
if (check == false) { // this will avoid map refreshing, I need only markers update.
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
check = true;
}
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
alert(locations[i]);
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i].trim()),
map: map
});
}
}
});}
问题是:未设置标记,控制台中没有错误。最近两天我正在处理这个问题,但找不到任何解决方案。你能帮助我吗?谢谢。
查看,地图对象在您的 infoWindow 变量中,尝试在那里设置位置。 这应该在位置循环内使用。
var pos = {
lat: latitude,
lng: longitude
};
infoWindow.setPosition(pos);
如您所见here in the docs,LatLng 对象与您使用的对象不同。 所以,替换:
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i].trim()),
map: map
});
有
locations[i] = locations[i].trim().split(',');
var latLng = {lat: Number(locations[i][0]), lng: Number(locations[i][1])};
marker = new google.maps.Marker({
position: latLng,
map: map
});