Page 1 of 1

Store locator

Posted: Mon May 09, 2016 10:05 am
by sleepydad
I am attempting to write a page that will generate hits of similar businesses within a specified radius. I found this https://developers.google.com/maps/arti ... l-with-php and it looks like what I need but it's not outputting as the example shows. Instead I get the error, "This XML file does not appear to have any style information associated with it. The document tree is shown below."

I've modified the code for my purposes as (NOTE I've named my table 'places'):

Code: Select all

$db=new mysqli('localhost','root','root','IDDB'); 

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

$query = sprintf("SELECT place_address, place_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 places HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
 $center_lat,
 $center_lng,
 $center_lat,
 $radius);
  
$result = $db->query($query);
header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each
for ($i=0;$i<$result->num_rows;$i++){
	$row=$result->fetch_assoc();
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name", $row['place_name']);
  $newnode->setAttribute("address", $row['place_address']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("distance", $row['distance']);
}

echo $dom->saveXML();
I've attached a screeengrab of my table here as well.

Any suggestions/advice would be greatly appreciated.

Re: Store locator

Posted: Mon May 09, 2016 10:09 am
by Celauran
sleepydad wrote:Instead I get the error, "This XML file does not appear to have any style information associated with it. The document tree is shown below."
I don't know that that's necessarily an error. You're sending XML to the browser, this is pretty standard behaviour. What is the desired/expected result?

Re: Store locator

Posted: Mon May 09, 2016 10:25 am
by sleepydad
Celauran wrote:
sleepydad wrote:Instead I get the error, "This XML file does not appear to have any style information associated with it. The document tree is shown below."
I don't know that that's necessarily an error. You're sending XML to the browser, this is pretty standard behaviour. What is the desired/expected result?
Ultimately what I want to do is laundry list the results as output on my web page something similar to:

Place One - Place One Address (Distance From $center_lat,$center_lng)
Place Two - Place Two Address (Distance From $center_lat,$center_lng)
Place Three - Place Three Address (Distance From $center_lat,$center_lng)

** Something along those lines.

Re: Store locator

Posted: Mon May 09, 2016 10:28 am
by Celauran
I'm not clear on where XML fits into that. Why not use HTML instead? The approach would be similar but rather than creating a DOM object, you could define some HTML, iterate over the query result set, and display in your template.

Re: Store locator

Posted: Mon May 09, 2016 11:19 am
by sleepydad
Celauran wrote:I'm not clear on where XML fits into that. Why not use HTML instead? The approach would be similar but rather than creating a DOM object, you could define some HTML, iterate over the query result set, and display in your template.
Duh :banghead: , excellent point. Ultimately the Google example was to generate a Google Map with a marker, so I'm guessing that's where the need for XML plays in. I won't have a need to generate the map for my purposes, so your solution of a simple HTML output is all I need.

Okay, so I've modified my code to:

Code: Select all

$db=new mysqli('localhost','root','root','IDDB'); 

$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];

$query="SELECT `place_name`,`lat`,`lng`, ( 3959 * acos( cos( radians(".$center_lat.") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(".$center_lng.") ) + sin( radians(".$center_lat.") ) * sin( radians( lat ) ) ) ) AS distance FROM `places` HAVING distance < ".$radius." ORDER BY distance LIMIT 0 , 20";

$result = $db->query($query);

for ($i=0;$i<$result->num_rows;$i++){
	$row=$result->fetch_assoc();
	echo $row['place_name']."<br>";
}
Getting a blank page. I'm not understanding 'distance' in the query. Perhaps that's the problem?

Thanks very much for your input (and patience). This query example from Google is unlike any I've written, so I'm still trying to wrap my head around it.

Re: Store locator

Posted: Mon May 09, 2016 11:34 am
by Celauran
Is error reporting enabled? Have you checked the value of $result?

Re: Store locator

Posted: Mon May 09, 2016 12:15 pm
by sleepydad
Celauran wrote:Is error reporting enabled? Have you checked the value of $result?
Got it figured out. It was working with the query that I posted last, but I was using rounded numbers manually typed in for my GET variables in the URL. So I put '38' for lat and '-122' for lng. I found that I needed to put the entire decimal number for each, ie 38.537281 for lat and 121.750946 for lng, for the calculation and comparison to work correctly.

My final, successful, code is

Code: Select all

$db=new mysqli('localhost','root','root','IDDB'); 

$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];

$query="SELECT
    *,
    ( 3959 * acos( cos( radians({$center_lat}) ) * cos( radians( `lat` ) ) * cos( radians( `lng` ) - radians({$center_lng}) ) + sin( radians({$center_lat}) ) * sin( radians( `lat` ) ) ) ) AS `distance`
FROM `places`
HAVING `distance` <= {$radius}
ORDER BY `distance` ASC limit 0,20";
$result=$db->query($query);

for ($i=0;$i<$result->num_rows;$i++){
	$row=$result->fetch_assoc();
	echo $row['place_name']."<br>";
}
Thanks again for all!