gpt4 book ai didi

php - 如何使用 sql 数据库向此代码添加谷歌地图信息面板

转载 作者:行者123 更新时间:2023-11-29 03:20:12 25 4
gpt4 key购买 nike

伙计们,我不知道如何在这段代码中为数据库中的每个点向我的标记添加一个信息面板。信息面板基本上可以有一个随机的字符串,我似乎无法弄清楚如何正确地实现它。我是使用谷歌地图 API 的新手,只是想不通,任何帮助都会被大大接受。 Output of markers `

          include 'connect.php';

$apikey = "AIzaSyAobypY52GXhqSDIMPhfoojHIe7lAKJnl8";
$id = $_GET['id'];

$lat = 0;
$long = 0;
$zoom = 8;

$findmap = "SELECT centerLat, centerLong, zoom FROM maps WHERE ID = $id";

if(!$result = $con->query($findmap)){
die('There was an error running the query [' . $con->error . ']');
} else {
$row = $result->fetch_assoc();
$lat = $row['centerLat'];
$long = $row['centerLong'];
$zoom = $row['zoom'];
}

?><!DOCTYPE html>
<html>
<head>
<meta name="viewport"
content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map-canvas { height: 100% }
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=
<?php echo $apikey; ?>&sensor=false">
</script>
<script type="text/javascript">
function initialize() {

var mapOptions = {
center: new google.maps.LatLng(<?php echo $lat.', '.$long; ?>),
zoom: <?php echo $zoom; ?>
};
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
var markers = [];
var infoWindows = [];
<?php
$getpoints = "SELECT pointLat, pointLong, pointText
FROM mappoints WHERE mapID = $id";

if(!$result = $con->query($getpoints)){
die('There was an error running the query
[' . $con->error . ']');
} else {
while ($row = $result->fetch_assoc()) {
echo ' var myLatlng1 = new google.maps.LatLng('.
$row[pointLat].', '.$row[pointLong].');
var infowindow = new google.maps.InfoWindow({
content:"'.$row[pointText].'"
});

var marker1 = new google.maps.Marker({
position: myLatlng1,
map: map,
title:"'.$row[pointText].'"
});



';

}
}
?>
markers.push(marker1);
infoWindows.push(new google.maps.InfoWindow);

var i;
for(i = 0; i < markers.length; i++) {
infowindow[i].setContent("content for infowindow");
infowindow[i].open(map, markers[i]);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<a href="Addtree.php">Add A tree</a>


<div id="map-canvas"/>

</body>
</html>

最佳答案

好的。您可以尝试这样的事情 ( reference )。这将加载多个 infoWindows,应该可以帮助您入门。

var map;
var markers = [];
var infoWindows = [];

/* Your map and marker creation code */

markers.push(marker1);
infoWindows.push(new google.maps.InfoWindow);

var i;
for(i = 0; i < markers.length; i++) {
infowindow[i].setContent("content for infowindow");
infowindow[i].open(map, markers[i]);
}

使用下面的代码

<?php
include 'connect.php';

$apikey = "*************************";
$id = $_GET['id'];

$lat = 0;
$long = 0;
$zoom = 8;

$findmap = "SELECT centerLat, centerLong, zoom FROM maps WHERE ID = $id";

if(!$result = $con->query($findmap)){
die('There was an error running the query [' . $con->error . ']');
} else {
$row = $result->fetch_assoc();
$lat = $row['centerLat'];
$long = $row['centerLong'];
$zoom = $row['zoom'];
}

?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport"
content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map-canvas { height: 100% }
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=
<?php echo $apikey; ?>&sensor=false">
</script>
<script type="text/javascript">
var markers = [];
var infoWindows = [];
var map;

function initialize() {

var mapOptions = {
center: new google.maps.LatLng(<?php echo $lat.', '.$long; ?>),
zoom: <?php echo $zoom; ?>
};
map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);

<?php
$getpoints = "SELECT pointLat, pointLong, pointText
FROM mappoints WHERE mapID = $id";

if(!$result = $con->query($getpoints)){
die('There was an error running the query
[' . $con->error . ']');
} else {
while ($row = $result->fetch_assoc()) {
?>
var myLatlng1 = new google.maps.LatLng(<?php echo $row['pointLat'] ?>, <?php echo $row['pointLong'] ?>);
var infowindow = new google.maps.InfoWindow;

var marker1 = new google.maps.Marker({
position: myLatlng1,
map: map,
title: "<?php echo $row['pointText']; ?>",
});

markers.push(marker1);
infoWindows.push(infowindow);
<?php
}
}
?>

var i;
for(i = 0; i < markers.length; i++) {
infowindow[i].setContent(markers[i].getTitle());
infowindow[i].open(map, markers[i]);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<a href="Addtree.php">Add A tree</a>


<div id="map-canvas"/>

</body>
</html>

关于php - 如何使用 sql 数据库向此代码添加谷歌地图信息面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46576593/

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