Re: [Google Maps API v3] Digest for google-maps-js-api-v3@googlegroups.com - 1 update in 1 topic

18 views
Skip to first unread message

Rob Banfield

unread,
Apr 26, 2017, 3:38:27 AM4/26/17
to google-map...@googlegroups.com
I suspect the problem comes from your creating a XML document, and specifying headers for it after content has been created. Might I suggest you drop the XML stuff until the very end if you really need it, and instead fill an array with your results returned from SQL, then use the array to create your output format: this could be JSON or XML or something else you may require. Something like this:

[code]<?php

  require("connect.php");
 
  // Get parameters from URL - these should be cleaned before use here!!!
  // Example:  $center_lat = MyCleaningFunction($_GET["lat"]);
  $center_lat = $_GET["lat"];
  $center_lng = $_GET["lng"];
  $radius = $_GET["radius"];
   
  // 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 id, name, address, 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 to array for each 
  $retVal = array();
  while ($row = @mysql_fetch_assoc($result)){
    $retVal[] = array(  "id"        => $row['id'],
                        "name"      => $row['name'],
                        "address"   => $row['address'],
                        "lat"       => $row['lat'],
                        "lng"       => $row['lng'],
                        "distance"  => $row['distance']  );    
  }
 
  // Return as JSON
  $json = json_encode($retVal);
  return $json; 
 
  // Return as XML...
  // Refer to e.g.  http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml
  // for various ways to do this. No need to create a new document with headers, etc....   
  $xml = MyArrayToXMLConverter($retVal);
 
?>[/code]

HTH, Rob


On Wed, Apr 26, 2017 at 8:30 AM, <google-map...@googlegroups.com> wrote:
Madhu Kumari <mk34...@gmail.com>: Apr 22 04:06AM -0700

i used this code but this is not give output,
 
its only give error msg:
This page contains the following errors:
error on line 2 at column 1: Extra content at the end of the document
Below is a rendering of the page up to the first error.
 
<?php
require("connect.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 id, name, address, 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);
$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("id", $row['id']);
$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();
?>
 
 
 
 
 
PLEASE HELP.
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to google-maps-js-api-v3+unsub...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages