gpt4 book ai didi

php - 如何使用数据库和 PHP 在 googlemap 上创建标记

转载 作者:行者123 更新时间:2023-11-30 22:35:45 24 4
gpt4 key购买 nike

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps AJAX + mySQL/PHP</title>
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">

var customIcons = {
restaurant: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
bar: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(47.6145, -122.3418),
zoom: 13,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP file
downloadUrl("maps_xml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
bindInfoWindow(marker, map, infoWindow, html);

}
});
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}

</script>
</head>

<body onload="load()">
<div id="map" style="width: 100%; height: 100%"></div>
</body>
</html>1

ma​​ps_xml.php这是我的 phpfile,它从数据库获取数据并动态创建标记。但是这样做我正在获取 map ,但即使插入了大约 7 个数据,我也能够在 map 上获得任何标记。请帮我解决这个问题。

<?php
require("config.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','&lt;',$htmlStr);
$xmlStr=str_replace('>','&gt;',$xmlStr);
$xmlStr=str_replace('"','&quot;',$xmlStr);
$xmlStr=str_replace("'",'&apos;',$xmlStr);
$xmlStr=str_replace("&",'&amp;',$xmlStr);
return $xmlStr;
}
// Opens a connection to a mySQL server
$connection=mysql_connect (localhost,$username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML('&','&amp;', $row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'type="' . $row['type'] . '" ';
echo '/>';
}
// End XML file
echo '</markers>';
?>

最佳答案

尝试以下添加小的更改:

// Select all the rows in the markers table
$query = "SELECT * FROM markers";
$results = mysql_query($query);
if (!$results) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n";
echo '<markers>' . "\r\n";
// Iterate through the rows, printing XML nodes for each
while ($row = mysql_fetch_assoc($results)){
// ADD TO XML DOCUMENT NODE
echo "\t<marker ";
echo 'name="' . parseToXML('&','&amp;', $row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'type="' . $row['type'] . '" ';
echo "/>\r\n";
}
// End XML file
echo "</markers>\r\n";

这将添加适合 XML 格式的正确的行尾和缩进。 JS 不会阅读它们,但这是一种很好的做法。然后,您还可以直接在浏览器中调用该页面来测试结果,如果 XML 正确,则您需要对 JS 代码进行故障排除并查看它不生成标记的原因。检查您的浏览器控制台(或使用 FireBug 等工具)查找错误。

编辑

发现<div id="map" style="width: 100%; height: 100%"></div>似乎是问题的一部分。建议改为像素值:<div id="map" style="width: 1024px; height: 680px;"></div> .

您也没有在 load() 中设置或更新您的标记阵列:

map.markers = map.markers || [];

再次在 downloadUrl() 中:

map.markers.push(marker);

工作示例:http://jsfiddle.net/Twisty/k5nnsqcL/

关于php - 如何使用数据库和 PHP 在 googlemap 上创建标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32593090/

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