Changing default location when no nearby stores

45 views
Skip to first unread message

tarscott

unread,
Jun 19, 2011, 4:08:38 PM6/19/11
to Google Maps JavaScript API v3
hi,

i have the store locator demo running (http://code.google.com/apis/
maps/articles/phpsqlsearch_v3.html).

it works, except when i geocode an address without a corresponding
nearby store location in database (<100 miles away), the map defaults
to lat 0, lng 180 (middle of Pacific Ocean).

just learning javascript. can anyone point in me in the right
direction for how to change this? i would like the map to default to
the original geocoded address even if there's no nearby location, like
the Ace example.

- tim

Martin™

unread,
Jun 20, 2011, 2:56:31 AM6/20/11
to Google Maps JavaScript API v3
Try this:

function searchLocationsNear(center) {
clearLocations();

var radius = document.getElementById('radiusSelect').value;
var searchUrl = 'phpsqlajax_search.php?lat=' + center.lat() +
'&lng=' + center.lng() + '&radius=' + radius;
downloadUrl(searchUrl, function(data) {
var xml = parseXml(data);
var markerNodes =
xml.documentElement.getElementsByTagName("marker");

if(markerNodes.length>0){

// heres the original code
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markerNodes.length; i++) {
var name = markerNodes[i].getAttribute("name");
var address = markerNodes[i].getAttribute("address");
var distance =
parseFloat(markerNodes[i].getAttribute("distance"));
var latlng = new google.maps.LatLng(
parseFloat(markerNodes[i].getAttribute("lat")),
parseFloat(markerNodes[i].getAttribute("lng")));

createOption(name, distance, i);
createMarker(latlng, name, address);
bounds.extend(latlng);
}
map.fitBounds(bounds);
// end of original code

} else {
// here set a default map view here as no nearby markers were found
}
});
}

It simply checks if there are any marker nodes returned to the map
from your database - if nodes exist then create the markers else set a
default map view.

Martin.

tarscott

unread,
Jun 20, 2011, 1:09:52 PM6/20/11
to google-map...@googlegroups.com
thanks, Martin!  will give it a try. 
Reply all
Reply to author
Forward
0 new messages