我使用{3}}的API v3制作了Google地图。
.php正在从数据库中提取结果,您可以看到this tutorial。
但是当应用于谷歌地图时,搜索结果没有结果,因为您可以看到here。
.html文件在搜索位置时没有从数据库中提取标记。我已经完成了100次代码,并在我错误的地方寻找网上的答案。 如果有人能为这个问题带来一些启示,我们将不胜感激。
这是.php
<?php
require("phpsqlsearch_dbinfo.php");
// Get parameters from URL
$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// 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());
}
// Search the rows in the markers table
$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));
$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name", $row['name']);
$newnode->setAttribute("address", $row['address']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("distance", $row['distance']);
}
echo $dom->saveXML();
?>
当我搜索半径25英里的德克萨斯州休斯顿时,这就是XML的用武之地。
<?xml version="1.0"?>
<markers><marker name="US Healthworks" address="9200 Hempstead, Houston, TX" lat="29.794104" lng="-95.449448" distance="5.34265857252369"/><marker name="Concentra Urgent Care" address="1000 N Post Oak, Houston, TX" lat="29.785303" lng="-95.456039" distance="5.47896700195167"/><marker name="US Healthworks" address="1414 S. Loop West, Houston, TX" lat="29.680426" lng="-95.399132" distance="5.79347704035615"/><marker name="Concentra Urgent Care" address="8799 North Loop E., Houston, TX" lat="29.797531" lng="-95.273872" distance="6.28265385500935"/><marker name="Doctor's Clinic Houston" address="6535 Southwest Freeway, Houston, TX" lat="29.717321" lng="-95.497299" distance="8.22610378379106"/><marker name="Northshore Occupational Medica" address="1140 Westmont Ste. 505, Houston, TX" lat="29.771984" lng="-95.195290" distance="10.4743938030692"/><marker name="US Healthworks" address="16630 Imperial, Houston, TX" lat="29.941263" lng="-95.396309" distance="12.615115596934"/><marker name="NOVA" address="6630 Roxburgh Drive, Houston, TX" lat="29.866304" lng="-95.555901" distance="13.371308778454"/><marker name="Fishbone Safety" address="208 X Street, Deer Park, TX" lat="29.694548" lng="-95.122627" distance="15.4859102039821"/><marker name="US Healthworks" address="17410 N.W. Freeway, Houston, TX" lat="29.888929" lng="-95.582146" distance="15.5495324355371"/><marker name="US Healthworks" address="10521 Corporate, Stafford, TX" lat="29.632973" lng="-95.596115" distance="16.2008784561048"/><marker name="Occupational Healthcare" address="610 S. Main Street, Highlands, TX" lat="29.808447" lng="-95.056885" distance="19.0351671183224"/><marker name="Medical Plaza Mobile Surveillance" address="10910 Spencer, La Porte, TX" lat="29.632973" lng="-95.062325" distance="20.4197847454651"/><marker name="US Healthworks" address="1309 West Fairmont Pkwy, LaPorte, TX" lat="29.651571" lng="-95.030998" distance="21.6516577038734"/></markers>
答案 0 :(得分:0)
<?php
require("phpsqlajax_dbinfo.php");///this is ure db username password etc
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;
}
// 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 $tablename";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<makers>';
// 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($row['name']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo '/>';
}
// End XML file
echo '</markers>';
答案 1 :(得分:0)
PHP脚本返回的XML文档格式不正确,因为它在第一行的XML声明之前包含空格字符:
<?xml version="1.0"?>
^
|-- This space character in front of the declaration is causing the problem.
这导致XML解析器失败并显示错误:XML declaration allowed only at the start of the document
。
它可能来自PHP标记<?php
和?>
周围的空格(包括空行)。
在xml
的第65行之后调试collection.html
变量以查看问题。我相信你可以通过删除XML声明之前的所有空格来解决这个问题。 [Reference]
另一方面,地图平移到海洋中部的原因是因为当没有标记添加到地图时,地图所适合的边界根本没有扩展 - 即{{1未到达第78行。
我建议修改bounds.extend(latlng)
函数以优雅地处理这种情况。
<强>更新强>
在下面的评论中,我们讨论了克服XML输出中的空格问题的方法。我想强调的是,最好的解决方案仍然是摆脱源代码中的空间,这是在PHP脚本中。
JavaScript中的searchLocationsNear()
函数没有广泛的浏览器支持(请参阅related SO question),并且实际上只解决症状,而不是解决问题的原因。