使用PHP文件和物理.XML文件生成的XML
我希望我问的是正确的方式,并提前感谢您的任何建议!我一直在尝试将本教程用于我自己网站上的一部分: 在尝试了我自己的代码的多个版本都没有成功之后,我决定继续一步一步地浏览教程-甚至创建一个新的MYSQL数据库,就像教程中一样 在教程示例中,他们使用实际的.xml文件从以下位置读取数据:使用PHP文件和物理.XML文件生成的XML,php,ajax,xml,Php,Ajax,Xml,我希望我问的是正确的方式,并提前感谢您的任何建议!我一直在尝试将本教程用于我自己网站上的一部分: 在尝试了我自己的代码的多个版本都没有成功之后,我决定继续一步一步地浏览教程-甚至创建一个新的MYSQL数据库,就像教程中一样 在教程示例中,他们使用实际的.xml文件从以下位置读取数据: // Change this depending on the name of your PHP or XML file downloadUrl('https://storage.googleapis.com/m
// Change this depending on the name of your PHP or XML file
downloadUrl('https://storage.googleapis.com/mapsdevsite/json/mapmarkers2.xml', function(data) {
但是,正如上面的注释行所说的代码“PHP文件”——这意味着您可以在那里使用PHP文件,而不是.xml文件,后者将根据DB查询动态生成xml代码
它们展示了从数据库中获取XML代码的3种不同方法,我尝试了所有3种方法,得到了相同的精确结果
- 当我运行index.html页面时,只会看到地图,根本没有任何标记
- 如果我只运行生成XML代码的php文件,浏览器中就不会显示任何内容(只是一个空白的白页)——但是——当我查看页面源代码时,它会完美地显示XML
- 如果我从PHP文件中获取生成的XML并将其复制/粘贴到一个.XML文件中,然后在downloadurl函数中调用该.XML文件,那么一切都会完美地工作
<!DOCTYPE html >
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Using MySQL and PHP with Google Maps</title>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var customLabel = {
restaurant: {
label: 'R'
},
bar: {
label: 'B'
}
};
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(-33.863276, 151.207977),
zoom: 12
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP or XML file
downloadUrl('convert-xml.php', function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function(markerElem) {
var id = markerElem.getAttribute('id');
var name = markerElem.getAttribute('name');
var address = markerElem.getAttribute('address');
var type = markerElem.getAttribute('type');
var point = new google.maps.LatLng(
parseFloat(markerElem.getAttribute('lat')),
parseFloat(markerElem.getAttribute('lng')));
var infowincontent = document.createElement('div');
var strong = document.createElement('strong');
strong.textContent = name
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = address
infowincontent.appendChild(text);
var icon = customLabel[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
label: icon.label
});
marker.addListener('click', function() {
infoWindow.setContent(infowincontent);
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>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=MY_KEY_IS_HERE&callback=initMap">
</script>
</body>
</html>
在Google地图中使用MySQL和PHP
/*始终明确设置贴图高度以定义div的大小
*包含映射的元素*/
#地图{
身高:100%;
}
/*可选:使示例页面填充窗口*/
html,正文{
身高:100%;
保证金:0;
填充:0;
}
var customLabel={
餐厅:{
标签:“R”
},
酒吧:{
标签:“B”
}
};
函数initMap(){
var map=new google.maps.map(document.getElementById('map'){
中心:新google.maps.LatLng(-33.863276151.207977),
缩放:12
});
var infoWindow=new google.maps.infoWindow;
//根据PHP或XML文件的名称进行更改
下载URL('convert-xml.php',函数(数据){
var xml=data.responseXML;
var markers=xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers,function(markereem)){
var id=markereem.getAttribute('id');
var name=markereem.getAttribute('name');
var address=markerem.getAttribute('address');
var type=markereem.getAttribute('type');
var point=new google.maps.LatLng(
parseFloat(markerem.getAttribute('lat')),
parseFloat(markerem.getAttribute('lng'));
var infowincontent=document.createElement('div');
var strong=document.createElement('strong');
strong.textContent=名称
infowincontent.appendChild(强);
infowincontent.appendChild(document.createElement('br'));
var text=document.createElement('text');
text.textContent=地址
infowincontent.appendChild(文本);
var icon=customLabel[type]| |{};
var marker=new google.maps.marker({
地图:地图,
位置:点,,
标签:icon.label
});
marker.addListener('click',function()){
setContent(infowincontent);
信息窗口。打开(地图、标记);
});
});
});
}
函数下载url(url,回调){
var请求=window.ActiveXObject?
新的ActiveXObject('Microsoft.XMLHTTP'):
新的XMLHttpRequest;
request.onreadystatechange=函数(){
if(request.readyState==4){
request.onreadystatechange=doNothing;
回调(请求、请求、状态);
}
};
打开('GET',url,true);
请求发送(空);
}
函数doNothing(){}
转换xml.php
<?php require 'conn-info.php';?>
<?php
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$dom->preserveWhiteSpace = false
$dom->formatOutput = true;
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// connect to db
$membermap_conn = mysql_connect($membermap_server, $membermap_user, $membermap_password, $membermap_database);
if ($membermap_conn->connect_errno) {
echo "Failed to connect to MySQL: (" . $membermap_conn->connect_errno . ") " . $membermap_conn->connect_error; }
// select the db
$db_selected = mysql_select_db($membermap_database, $membermap_conn);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Pull the zip / user information out of the database.
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// Add to XML document node
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("id",$row['id']);
$newnode->setAttribute("name",$row['name']);
$newnode->setAttribute("address", $row['address']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("type", $row['type']); }
echo $dom->saveXML();
?>
保留空格=false
$dom->formatOutput=true;
$node=$dom->createElement(“标记”);
$parnode=$dom->appendChild($node);
//连接到数据库
$membermap\u conn=mysql\u connect($membermap\u server、$membermap\u user、$membermap\u password、$membermap\u database);
如果($membermap\u conn->connect\u errno){
echo“无法连接到MySQL:(“$membermap\u-conn->connect\u-errno.”“$membermap\u-conn->connect\u-error;”)
//选择数据库
$db\u selected=mysql\u select\u db($membermap\u database,$membermap\u conn);
如果选择了(!$db_){
die('不能使用db:'.mysql_error());
}
//从数据库中提取zip/用户信息。
$query=“从标记中选择*,其中1”;
$result=mysql\u query($query);
如果(!$result){
die('无效查询:'.mysql_error());
}
标题(“内容类型:text/xml”);
//遍历行,为每个行添加XML节点
而($row=@mysql\u fetch\u assoc($result)){
//添加到XML文档节点
$node=$dom->createElement(“标记”);
$newnode=$parnode->appendChild($node);
$newnode->setAttribute(“id”,$row['id']);
$newnode->setAttribute(“名称”,$row['n