0

うーん、基本的に私は PHP に比較的慣れていないので、チュートリアルを試してみました (以下のリンクを参照)。

Chrome を使用して得たエラー: このページには次のエラーが含まれています: 行 1、列 3 のエラー: xmlParsePI : ターゲット名がありません 以下は、最初のエラーまでのページのレンダリングです。

チュートリアル: https://developers.google.com/maps/articles/phpsqlsearch_v3

これはコードです:

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

// Get parameters from URL
// Get parameters from URL
$center_lat = ( isset( $_GET["lat"] ) ? $_GET["lat"] : 37 ); 
$center_lng = ( isset( $_GET["lng"] ) ? $_GET["lng"] : 122 ); 
$radius     = ( isset( $_GET["radius"] ) ? $_GET["radius"] : 25 );

// 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', 'root', '');
if (!$connection) {
  die("Not connected : " . mysql_error());

}

// Set the active mySQL database
$db_selected = mysql_select_db('test', $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());
}

header("Content-type: text/xml");

// 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();
?>

誰でも助けてもらえますか?

4

1 に答える 1

1

チュートリアルから、経度座標は負の値です

Frankie Johnnie & Luigo Too,"939 W El Camino Real, Mountain View, CA",37.386339,-122.085823

あなたのコードでは、デフォルトの経度座標は正です。

変化する

$center_lng = ( isset( $_GET["lng"] ) ? $_GET["lng"] : 122 ); 

 $center_lng = ( isset( $_GET["lng"] ) ? $_GET["lng"] : -122 ); 

修正したコードをテストしたところ、xml が生成されました。

ここに私の完全なコードがあります

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

// Get parameters from URL
$center_lat = ( isset( $_GET["lat"] ) ? $_GET["lat"] : 37 ); 
$center_lng = ( isset( $_GET["lng"] ) ? $_GET["lng"] : -122 ); 
$radius     = ( isset( $_GET["radius"] ) ? $_GET["radius"] : 25 );

// 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 ($host, $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());
}

header("Content-type: text/xml");

// 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();
?>

これが結果の画像です ここに画像の説明を入力

于 2013-11-05T16:41:37.993 に答える