Search always goes to Pacific Ocean

689 views
Skip to first unread message

Am

unread,
Oct 14, 2011, 2:34:08 PM10/14/11
to google-map...@googlegroups.com
Hi, I'm having trouble getting the sample code from the store locator page to work. I copied the HTML code from this page: http://gmaps-samples-v3.googlecode.com/svn/trunk/articles/phpsqlsearch/phpsqlsearch_map.html, and uploaded it to my own page. Every search I try takes me to the middle of the Pacific Ocean rather than the correct location on the map. My page is located here: http://www.rentalmatch101.com.php5-20.dfw1-1.websitetestlink.com/googlemap.php. I didn't notice any errors.
What did I do wrong? I just copied and pasted the code on the sample page exactly as it was.
Thanks

BruceB

unread,
Oct 14, 2011, 5:15:57 PM10/14/11
to google-map...@googlegroups.com
There's something wrong with your PHP search app (phpsqlsearch_genxml.php), because it's returning an empty XML document.  So in your callback function the bounds object is initialized but never set with any other values, so it's defaulting to the Pacific Ocean.
which returns no data, so markerNodes is an empty array.

geoco...@gmail.com

unread,
Oct 14, 2011, 6:07:16 PM10/14/11
to Google Maps JavaScript API v3
On Oct 14, 2:15 pm, BruceB <bru...@google.com> wrote:
> There's something wrong with your PHP search app (phpsqlsearch_genxml.php),
> because it's returning an empty XML document.  So in your callback function
> the bounds object is initialized but never set with any other values, so
> it's defaulting to the Pacific Ocean.
> For example, a search for "texas" generates this searchUrl:
>  http://www.rentalmatch101.com.php5-20.dfw1-1.websitetestlink.com/phps...
> which returns no data, so markerNodes is an empty array.

However there is data in the database:
http://www.rentalmatch101.com.php5-20.dfw1-1.websitetestlink.com/phpsqlsearch_genxml.php?lat=31.9685988&lng=-99.90181310000003&radius=25000

They are just not easy to find with a radius of 25 miles:
http://www.geocodezip.com/GenericMapBrowser.asp?filename=http://www.geocodezip.com/xmlProxy060215.asp?http%3A%2F%2Fwww.rentalmatch101.com.php5-20.dfw1-1.websitetestlink.com%2Fphpsqlsearch_genxml.php%3Flat%3D31.9685988%26lng%3D-99.90181310000003%26radius%3D25000

(and some of them have invalid lat/lng coordinates)

The OP needs to implement better error handling on the page...

-- Larry

geoco...@gmail.com

unread,
Oct 14, 2011, 6:21:34 PM10/14/11
to Google Maps JavaScript API v3
On Oct 14, 3:07 pm, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
> On Oct 14, 2:15 pm, BruceB <bru...@google.com> wrote:
>
> > There's something wrong with your PHP search app (phpsqlsearch_genxml.php),
> > because it's returning an empty XML document.  So in your callback function
> > the bounds object is initialized but never set with any other values, so
> > it's defaulting to the Pacific Ocean.
> > For example, a search for "texas" generates this searchUrl:
> >  http://www.rentalmatch101.com.php5-20.dfw1-1.websitetestlink.com/phps...
> > which returns no data, so markerNodes is an empty array.
>
> However there is data in the database:http://www.rentalmatch101.com.php5-20.dfw1-1.websitetestlink.com/phps...
>
> They are just not easy to find with a radius of 25 miles:http://www.geocodezip.com/GenericMapBrowser.asp?filename=http://www.g...
>
> (and some of them have invalid lat/lng coordinates)

These three:
<marker address="123 Somewhere Drive" lat="0" lng=""
distance="6798.3918880607"/>
<marker address="" lat="0" lng="" distance="6798.3918880607"/>
<marker address="" lat="0" lng="" distance="6798.3918880607"/>

-- Larry

Am

unread,
Oct 17, 2011, 12:24:34 PM10/17/11
to google-map...@googlegroups.com
Thank you for all the help. How do I make it so when I search for something that isn't in the database, it goes to the area on the map without markers, instead of the ocean? Meaning, if I search for Colorado, it goes to Colorado on the map and not the Pacific Ocean? Or just zoom out the map enough to list the nearest marker, even if it's 1000 miles away? So if I search for Colorado, it shows me Colorado and California? OR or just pop up an error message saying "No properties within that search radius"?
Thanks

BruceB

unread,
Oct 17, 2011, 2:54:07 PM10/17/11
to google-map...@googlegroups.com
Might be a design choice, but if it were me I would simply check for zero results from your search service, and if there are no results I would center the map on the lat/lng that was returned from the geocoder (i.e. save the geocode response in a variable that you can access from your search service callback).  You could also expand the search radius in a step-wise fashion, but it might be useful to signal the user that you're doing so -- otherwise they might get annoyed with the wait time (if there's no indication of what's going on).  But you'll want to put a maximum search radius into your logic, or you might end up in an endless loop.

davie strachan

unread,
Oct 17, 2011, 4:48:24 PM10/17/11
to Google Maps JavaScript API v3
Hi
You could change your PHP file to return XML result centered on
Geolocation results using mysql_num_rows()

$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}
header("Content-type: text/xml");
if (mysql_num_rows($result)==0)
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name", "No Records Found");
$newnode->setAttribute("address","Centered on Location);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("distance",0);
else {
// 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", $center_lat);
$newnode->setAttribute("lng", $center_lng);
$newnode->setAttribute("distance", $row['distance']);
}
}
I have not tried this out yet
Regards Davie

I

davie strachan

unread,
Oct 17, 2011, 4:51:34 PM10/17/11
to Google Maps JavaScript API v3
Hi
Sorry a Senior Moment

$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}
header("Content-type: text/xml");
if (mysql_num_rows($result)==0)
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name", "No Records Found");
$newnode->setAttribute("address","Centered on Location);
$newnode->setAttribute("lat", $center_lat);
$newnode->setAttribute("lng", $center_lng);
$newnode->setAttribute("distance", 0);

else {
// 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']);
}
}

Regards Davie

wang tiezhen

unread,
Oct 17, 2011, 4:52:09 PM10/17/11
to google-map...@googlegroups.com
Maybe lat and lng are reversed?

2011/10/17 davie strachan <davies...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


Am

unread,
Oct 21, 2011, 9:35:39 AM10/21/11
to google-map...@googlegroups.com
Thank you for the code. I added the code to my phpsqlsearch_genxml.php. I'm still having the issue with it going to the ocean. Is there a specific place in the code that I need to put that code? I put it at the end of the code. Could that be why it's not working?

Am

unread,
Oct 21, 2011, 9:49:34 AM10/21/11
to google-map...@googlegroups.com
Also, once the map goes to the ocean, I can't make another search. It gets "stuck" in the ocean. I have to shift + refresh to search again. What could be causing this issue?
Thank you for your help.

Am

unread,
Oct 26, 2011, 11:03:02 AM10/26/11
to google-map...@googlegroups.com
Is there anywhere specific in my code I'm supposed to add this? Before or after anything?

davie strachan

unread,
Oct 26, 2011, 12:27:47 PM10/26/11
to Google Maps JavaScript API v3
Hi
I tried http://www.rentalmatch101.com.php5-20.dfw1-1.websitetestlink.com/phpsqlsearch_genxml.php?lat=40&lng=-100&radius=2500
this produced XML for 20 properties. The last 2 having no proper lat/
lng.
I would suggest you ensure that the data in the database is accurate
and that there is sufficient records to test (for USA + Canada I
personally would need at least 1000)
The coding goes in the phpsqlsearch_genxml.php file replacing the XML
generting lines

Regards Davie

Am

unread,
Oct 26, 2011, 2:42:23 PM10/26/11
to google-map...@googlegroups.com
Thank you for the input. I don't have 1000 properties, and may never have properties in every country. Other store locators don't have thousands of properties or properties in other countries, but doesn't default to the center of the ocean when you search for a location without properties.

With mine, if I search for someplace that doesn't have a property, the map goes to the ocean. How do I make it go to the location they searched for on the map without markers, or zoom out enough to show their searched location and the closest property, even if it's 1000 miles away?

I'd like it to work similar to Ace Hardware's store locator. http://www.acehardware.com/mystore/storeLocator.jsp If I search for a location that doesn't have any stores, it goes to that location on the map without markers.

Does this make sense?

Rossko

unread,
Oct 26, 2011, 3:41:42 PM10/26/11
to Google Maps JavaScript API v3
> Does this make sense?

Yes, it does. You'll have to write code to make it behave the way you
want.

Try this in your browser
http://www.rentalmatch101.com.php5-20.dfw1-1.websitetestlink.com/phpsqlsearch_genxml.php?lat=40&lng=-100&radius=1
It's a search that should return no properties.

When your map script tries to read XML like that, with no useful
content, it blows up because no-one told it what to do.

'Defensive programming' is about writing code that can handle that
kind of exception, and deal with it in a predictable way.

You can enhance your map script to test for valid data, and if there
isn't then do whatever you want it to do.
Or, you could as well / instead enhance your php code to always return
something in the XML even for unsuccessful searches, which is what
davie suggested.

Am

unread,
Oct 26, 2011, 4:17:20 PM10/26/11
to google-map...@googlegroups.com
Thank you for the explanation. How do I write the code? What do I write? Is there a tutorial to walk me through this? I don't know what code needs to be written.
Thanks for your help.

davie strachan

unread,
Oct 27, 2011, 3:22:10 AM10/27/11
to Google Maps JavaScript API v3
Hi again
1.Make sure your database contains valid lat & lng values
2.Replace
$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']);
}


With
$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}

header("Content-type: text/xml");
if (mysql_num_rows($result)==0)
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name", "No Records Found");
$newnode->setAttribute("address","Centered on
Location);
$newnode->setAttribute("lat", $center_lat);
$newnode->setAttribute("lng", $center_lng);
$newnode->setAttribute("distance", 0);

else {
// 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']);
}

}
Regards Davie

Am

unread,
Oct 27, 2011, 11:16:03 AM10/27/11
to google-map...@googlegroups.com
thank you so much for your help. I appreciate it
Reply all
Reply to author
Forward
0 new messages