gpt4 book ai didi

php - 如何在谷歌地图标记上动态显示数据库值?

转载 作者:行者123 更新时间:2023-11-29 19:34:45 26 4
gpt4 key购买 nike

这是我从数据库获取记录并显示在线用户的代码

$markers = array();         
$sql_locations1="SELECT DISTINCT uid,latitude,longitude from `location` where uid='$_REQUEST[locator_id]' ORDER by `date_time` DESC limit 1";
$result1 = mysqli_query($conn,$sql_locations1);
while($row=mysqli_fetch_assoc($result1))
{
$markers[] = array(
'abc',
$row['latitude'],
$row['longitude'],
);
}?>
<div id="map_canvas" onload="initMap()"></div>

正如您在数组中看到的“abc”,它的静态值显示在 map 上的每个标记上。我想要的是来自数据库的用户名,在他们自己的标记上代替“abc”静态值我不知道如何在那个地方获取这个数据库值。下面是我的 map 函数

<script>
function initialize() {
var map;
var bounds = new google.maps.LatLngBounds();
var mapOptions = {
mapTypeId: "roadmap",
center: new google.maps.LatLng(20.5937, 78.9629), // somewhere in the uk BEWARE center is required
zoom: 1,
};
// Display a map on the page
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
map.setTilt(45);
// Multiple Markers
var markers = <?php echo json_encode( $markers );?>;
// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow();
var marker, i;
// Loop through our array of markers & place each one on the map
for (i = 0; i < markers.length; i++) {
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0]
});
// Allow each marker to have an info window
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infoWindow.setContent(infoWindowContent[i][0]);
infoWindow.open(map, marker);
}
})(marker, i));

// Automatically center the map fitting all markers on the screen
map.fitBounds(bounds);
}
//Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function (event) {
this.setZoom(5);
google.maps.event.removeListener(boundsListener);
});
}
google.maps.event.addDomListener(window, 'load', initialize);

请提前帮助我同样的感谢

最佳答案

您可以编写一个连接查询来从数据库中获取用户名并将其显示在 map 的信息窗口中:

// Allow each marker to have an info window
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infoWindow.setContent(markers[i][0]);
infoWindow.open(map, marker);
}
})(marker, i));

关于php - 如何在谷歌地图标记上动态显示数据库值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41605527/

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