这里完全被难住了。无法正确显示 '(撇号)...使用来自 google Google Maps Tutorial 的教程从我的数据库创建 map 。它连接到我的数据库,创建一个 XML 文件,然后引用该 XML 文件来创建 map 。
以下代码是我创建 XML 的 PHP 文件。查看转义码。
<?php
// Escape Characters
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
$servername = "localhost";
$username = "XXX";
$password = "XXX";
$dbname = "XXX";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Select all the rows in the markers table
$sql = "
SELECT *
FROM table1
WHERE status = 'ACTIVE'";
$result = $conn->query($sql);
if (!$result) {
die('Invalid query: ' . mysqli_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo "<?xml version='1.0' ?>";
echo '<markers>';
$ind=0;
// Iterate through the rows, printing XML nodes for each
while ($row = @mysqli_fetch_assoc($result)){
// Add to XML document node
echo '<marker ';
echo 'fac_id="' . $row['fac_id'] . '" ';
echo 'fac_name="' . parseToXML($row['fac_name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'region="' . $row['region'] . '" ';
echo '/>';
$ind = $ind + 1;
}
// End XML file
echo '</markers>';
?>
这是上面创建的 XML 文件中的一行。撇号表示为 '
<marker fac_id="123" fac_name="St. Luke's MC" address="1800 East Van Buren Street" lat="33.451542" lng="-112.043129" region="West C"/>
下面是用于创建实际 map 的 html 文件(因为我删除了个人数据而无法使用)。我真的没有 javascript 经验,所以我不确定为什么没有解码撇号?
不正确显示的撇号的屏幕截图: Google Map Screenshot
<!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 = {
'East A': {
label: 'A'
},
'West C': {
label: 'C'
},
'North D': {
label: 'D'
},
'East K': {
label: 'K'
},
'North M': {
label: 'M'
},
'South S': {
label: 'S'
}
};
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(35.845424, -93.738202),
zoom: 4.75
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP or XML file
downloadUrl('http://example.com/xmlmaker.php', function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function(markerElem) {
var fac_id = markerElem.getAttribute('fac_id');
var fac_name = markerElem.getAttribute('fac_name');
var address = markerElem.getAttribute('address');
var region = markerElem.getAttribute('region');
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 = fac_name
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = address
infowincontent.appendChild(text);
var icon = customLabel[region] || {};
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=ABC1234567890&callback=initMap">
</script>
</body>
</html>
最佳答案
您的 parseToXML($htmlStr)
函数正在从较早的替换中删除 &
替换,因为在它用 '
替换 之后'
,然后它通过将 &
替换为 &
来破坏 '
。
不必在所有上下文中替换所有这些字符。参见 Simplified XML Escaping ,并优化您的 parseToXML()
函数以及相应调用它的位置。
关于javascript - Google map 转义字符未正确显示 ' - XML 和 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49388454/