php - 使用 Google Maps API 检查 XML

标签 php mysql google-maps

过去几天我一直在研究 Google Maps API。由于缺乏 PHP 技能,我陷入了困境。运行下面的 PHP 代码后,返回的 XML 为:

<?xml version="1.0"?>
<markers/>

我使用的链接是:http://www.icliqz.com/Dev/phpsqlsearch_genxml_php.php?latitude=41.782321&longitude=-72.612031&radius=50

我正在使用的代码来自:http://code.google.com/apis/maps/articles/phpsqlsearch.html

我正在使用 Dreamweaver 来处理这个项目。如果有一种通过 PHP 代码进行调试的好方法,我将非常感激。我尝试用谷歌搜索它,但我不确定什么对 PHP 新手来说是最好的。 我检查了数据库中记录的半径。它们都在我测试的坐标 50 英里范围内。

PHP 代码是:

<?php
require("phpsqlsearch_dbinfo.php");

// Start XML file, create parent node
$doc = new DOMDocument("1.0");
$node = $doc->createElement("markers");
$parnode = $doc->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());
}

// Select all the rows in the markers table
$query = "SELECT user, latitude, longitude, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM tbl_UserLatLng HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20";
$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 = $doc->createElement("marker");
  $newnode = $parnode->appendChild($node);

  $newnode->set_attribute("user", $row['user']);
  $newnode->set_attribute("latitude", $row['latitude']);
  $newnode->set_attribute("longitude", $row['longitude']);
  $newnode->set_Attribute("distance", $row['distance']);
}

echo $doc->saveXML();

?>

提前致谢!

最佳答案

尝试从 mysql shell 手动运行查询以查看显示的距离值。您似乎没有从数据库中获得任何结果。

SELECT user, latitude, longitude, 
    ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - 
    radians('%s') ) + 
    sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance 
FROM tbl_UserLatLng

关于php - 使用 Google Maps API 检查 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7591377/

相关文章:

PHP : How to calculate the remaining time in minutes?

php - 如何检查 UPDATE mysqli 查询是否正确执行?

mysql - MySQL中如何限制其他用户删除root用户?

php - 添加准备语句会导致警告

php数组通过复制值或引用分配?

php - 将逗号分隔的 mysql 标签与数据库值相匹配 (WP - PHP)

mysql - 如何在 MySQL/Rails 中获取列的随机默认值

javascript - 谷歌地图标记不递增数组

ios - 指向 maps.google.com 的 iPhone HTML 链接在 Mobile Safari 而非 Maps 应用程序中打开页面

android - MapView 和 Expandablelistview 滚动