所以我试图在从我的数据库中获取的谷歌地图上显示数据(使用 API 3)。我得到了正确居中的谷歌地图,但没有标记。
generateXml.php 和 ViewMap.php 都在同一个目录中。
请帮我appriciate它...
非常感谢你。
生成XML.php
<?php
require("db_connection.php");
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;
}
// Select all the rows in the markers table
$query = "SELECT transportType,costPerKm,address,teleNo,webLink,description,lat,lng FROM transportpublic";
$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 'transportType="' . parseToXML($row['transportType']) . '" ';
echo 'costPerKm="' . $row['costPerKm'] . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'teleNo="' . $row['teleNo'] . '" ';
echo 'webLink="' . parseToXML($row['webLink']) . '" ';
echo 'description="' . parseToXML($row['description']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo '/>';
}
// End XML file
echo '</markers>';
?>
<markers>
<marker transportType="abc" costPerKm="1.50" address="abc" teleNo="112554476" webLink="http://www.abc.html" description="abc" lat="6.886464" lng="79.871287"/>
<marker transportType="Train" costPerKm="1.50" address="abc" teleNo="112541258" webLink="http://www.abc.html" description="Good" lat="7.284459" lng="80.637459"/>
</markers>
视图地图.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>View Map</title>
<link rel="stylesheet" type="text/css" href="template/css/style.css" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map { height: 100% }
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="util.js"></script>
<script type="text/javascript">
var infowindow;
var map;
function initialize()
{
var myLatlng = new google.maps.LatLng(6.796396,79.877823);
var myOptions = {
zoom: 13,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("mapC"), myOptions);
var transitLayer = new google.maps.TransitLayer();
transitLayer.setMap(map);
downloadUrl("generateXml.php", function(data)
{
data = xmlParse(data);
var markers = data.documentElement.getElementsByTagName("marker");
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; i++)
{
var latlng = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
bounds.extend(latlng);
var marker = createMarker(markers[i].getAttribute("transportType"), latlng);
}
map.fitBounds(bounds);
});
}
function createMarker(transportType, latlng)
{
var marker = new google.maps.Marker({position: latlng, map: map});
google.maps.event.addListener(marker, "click", function() {
if (infowindow) infowindow.close();
infowindow = new google.maps.InfoWindow({content: transportType});
infowindow.open(map, marker);
});
return 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="initialize()">
<div id="content123">
<div style="height:400px">
<!--dispaly map -->
<div id="mapC" style="width: 100%; height: 400px"></div>
</div>
</div>
</body>
</html>
当这一行添加到 mapView.php
<?php include("generateXml.php")?>
它甚至不显示地图它给了我错误
This page contains the following errors:
error on line 57 at column 26: StartTag: invalid element name
Below is a rendering of the page up to the first error.
View Map html { height: 100% }
body { height: 100%; margin: 0; padding: 0 } #map { height: 100% }
var infowindow; var map;
function initialize()
{
var myLatlng = new google.maps.LatLng(6.886464,79.871287);
var myOptions = { zoom: 10, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP}
map = new google.maps.Map(document.getElementById("mapC"), myOptions);
var transitLayer = new google.maps.TransitLayer(); transitLayer.setMap(map);
downloadUrl("generateXml.php", function(data)
{ var xml = data.responseXML;
var markers = data.getElementsByTagName("marker");
var bounds = new google.maps.LatLngBounds(); for (var i = 0; i