-2

I made a Google Map with API v3 with this tutorial.

The .php is pulling results from the DB as you can see here.

But when applied to with the google map searching comes back with no results as you can see here.

The .html file isn't pulling the markers from the DB when searching for a location. Ive been over the code 100 times and looked for answers across the net on where I went wrong. If someone could bring some light to this issue it would be greatly appreciated.

Here is the .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();
?>

This is what the XML is putting out when I search Houston, TX in a 25 mile radius.

 <?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>
NathanD
  • 5
  • 4
  • Is the search box working? It brings me to the middle of the ocean when I hit the Search button. – Andrew Nov 15 '12 at 19:07
  • That's the issue, I cant figure out why its not pulling the marker info from the db. The .php file alone is, but the .html that is using the .php file isn't. – NathanD Nov 15 '12 at 19:30
  • There is a space character at the beginning of the XML document returned from the .php code. Try removing that space. If you debugged `collection.html`, you should see that in line 65 (`var xml = parseXml(data)`), the XML cannot be parsed due to an error: "error on line 1 at column 6: XML declaration allowed only at the start of the document" – Andrew Nov 15 '12 at 19:35
  • Have you tried using the original PHP code and just making sure there aren't any whitespaces before the XML declaration? I think it should work. – Andrew Nov 15 '12 at 20:59

2 Answers2

0
   <?php
require("phpsqlajax_dbinfo.php");///this is ure db username password etc

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$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>';
Dnaso
  • 1,335
  • 4
  • 22
  • 48
  • Are you sure? I get an XML document with markers. view-source:http://testforhealth.net/TCA/phpsqlsearch_genxml.php?lat=33&lng=-86&radius=50 – Andrew Nov 15 '12 at 19:07
  • http://testforhealth.net/TCA/phpsqlsearch_genxml.php?lat=33&lng=-86&radius=50 View the page source, its pulling markers from the db.But for some reason I cant figure out the .html is not pulling the markers from the db. – NathanD Nov 15 '12 at 19:26
  • @user1827526 I wonder did you try only SQL? Didn't that work? – wf9a5m75 Nov 15 '12 at 19:37
  • weird i see your problem though, you have markers inside markers and your not closing your tags right. post the php you have because i can see it when i veiw source but its not actually coming up. to summarize you have an object closing issue where something is getting created inside another maker, so just post the php and ill fix it for you – Dnaso Nov 15 '12 at 19:47
  • @Dnaso The `marker` tag is an "empty tag" containing no child tags, and thus can be closed by adding a slash at the end (i.e. `/>`). I don't believe that's a problem here. – Andrew Nov 15 '12 at 19:55
  • no where he is doing the markers object then – Dnaso Nov 15 '12 at 19:57
  • @andrewap check it out and you will see what I am talking about ^ – Dnaso Nov 15 '12 at 20:01
  • I see what you described @Dnaso, but Chrome is not interpreting it as XML. In the Elements inspector, you even see `` and `` tags being inserted. If you saved the output as a .xml file (with the whitespace removed in the first line), and then view the file using Chrome, you'll see that the XML is well-formed. – Andrew Nov 15 '12 at 20:29
  • That broke the .php all togeather. Its not even showing results in page source of the .php – NathanD Nov 15 '12 at 20:38
  • ill put in the full php to use, give me 5 min – Dnaso Nov 15 '12 at 20:43
  • THIS 100 PERCENT WORKS, i use it myself – Dnaso Nov 15 '12 at 20:46
  • just throw in the haversine sprintf and also make a row for the distance nodes and your set to go – Dnaso Nov 15 '12 at 20:47
  • I'm not sure why this solution is better, as I don't think the problem lies in the actual XML tags. @user1827526's original PHP code uses the `DOMDocument` class, which is much cleaner and less error-prone than manually constructing and escaping the XML markup. – Andrew Nov 15 '12 at 21:06
  • @andrewap I never said it was, but its obvious he is having problems and my solution and mark up is Alot easier to debug , because once he implements it I'm sure e will want to add or drop stuff... – Dnaso Nov 15 '12 at 21:37
  • I'm going to put his php on my server to see what I get though, so he can use his original document class php – Dnaso Nov 15 '12 at 21:39
0

The XML document returned by your PHP script is not well-formed, as it contained a space character before the XML declaration in the first line:

 <?xml version="1.0"?>
^
|-- This space character in front of the declaration is causing the problem.

This caused the XML parser to fail with an error: XML declaration allowed only at the start of the document.

It could have come from whitespaces (including empty lines) around the PHP tags <?php and ?>.

Debug the xml variable after line 65 in collection.html to see the problem. I believe you can fix this by removing all whitespaces before the XML declaration. [Reference]


On the other hand, the reason why the map is panned to the middle of the ocean is because when no markers are added to the map, the bounds to which the map is fitted is not extended at all — i.e. bounds.extend(latlng) on line 78 is not reached.

I suggest modifying the searchLocationsNear() function to gracefully handle this situation.


UPDATE:

In the comments below, we discussed ways to overcome the issue with whitespaces in the XML output. I want to emphasize that the best solution is still to get rid of the space at the source, which is in the PHP script.

The trim() function in JavaScript does not have widespread browser support (see related SO question), and is really addressing only the symptoms but not the cause of the problem.

Community
  • 1
  • 1
Andrew
  • 2,770
  • 1
  • 22
  • 29
  • Thanks Andrew for all the comments. Could you elaborate more, I'm a self taught amateur (Probably why I cant get this working). Exactly what whitespace are you referring to. Plz forgive my lack of knowledge.. I'm trying to learn :) – NathanD Nov 15 '12 at 21:44
  • No problem. I edited my answer. It's a single space character that's causing the XML parser to fail. – Andrew Nov 15 '12 at 22:04
  • I deleted those white-spaces. And their was no change. I've just used a debug in Firefox on the map when searching. Come to find out its pulling the data from the DB and working properly. But its just not applying the data it receives to the map. So what that tells me the .php is working fine, it's something in the .html that isn't. Correct me if I'm wrong – NathanD Nov 15 '12 at 22:29
  • I still see the space in front of the XML declaration (in fact you should see it if you view the source). I have two suggestions: i) hunt down the cause of that space character in your .php file; OR ii) remove the whitespace before the XML is parsed by using `var xml = parseXml($.trim(data))` in line 65 of the .html. – Andrew Nov 15 '12 at 22:36
  • By the way, I am fairly certain that the whitespace is causing the issue. – Andrew Nov 15 '12 at 22:37
  • I replaced line 65 with xml = parseXml($.trim(data)) and got this error. ReferenceError: $ is not defined http://testforhealth.net/TCA/collection.html Line 65. And the xml is no longer putting out any info. You can try it yourself its updated. – NathanD Nov 15 '12 at 22:39
  • Replace it, so that any whitespaces before and after are removed by the `trim` function before being parsed. – Andrew Nov 15 '12 at 22:41
  • The way it was.. no error was showing up on my end. And the info from the xml was being brought in, just not being applied to the map. EDITED comment above and updated question with the xml results i got before the change. – NathanD Nov 15 '12 at 22:45
  • And sorry, my mistake; I forgot you're not using jQuery. In this case, you should change the line to `var xml = parseXml(data.trim())` for now. – Andrew Nov 15 '12 at 22:46
  • Your my HERO! That did it! I will be using jQuery on final version once its applied to the site, so should i change it to that then? – NathanD Nov 15 '12 at 22:48
  • Great! I updated my answer about removing whitespaces. I would still suggest you find out why there is an extra space in the XML output. Once you get rid of it at the source, you'll no longer need the `trim()` function. But if that's not possible, yes, please use the jQuery `$.trim()` function instead. – Andrew Nov 15 '12 at 22:54
  • The whitespace would be in the php correct? If it can cause problems in some browsers then ill have to hunt it down. Any advice on what to look for? – NathanD Nov 15 '12 at 22:57
  • Yes, the sneaky space came from the PHP script, somewhere. Look for empty lines and empty spaces. Especially look before the ` – Andrew Nov 15 '12 at 23:01
  • I found it. So no trim command needed now. Once again ty for your time and help.. 3 days of me trying to figure this out and its because of some whitespace somewhere in the php lol I would have been forever lost without you. – NathanD Nov 15 '12 at 23:14
  • Awesome! I'm glad to help. It is astounding how one little space character in the wrong place can cause so much trouble... – Andrew Nov 15 '12 at 23:19