Forum Moderators: open
I have a map centering/zoom issue. The problem I run into is when my search returns 0 results.
Within function load() is a statement that centers my map on the USA (zoomed out):
map.setCenter(new GLatLng(40, -100), 3);
So we are off to a great start, except that the very next line calls showMap() which re-centers the map and zoom to fit the incoming data points (in this case, there are none). This command ends up centering over the Pacific Ocean and zooming out to a "world view" level. (Note, when I do pass in marker locations in the XML file, everything zooms appropriately...no problem there.)
I am sure that there is some easy conditional statemene that could go somewhere to check if there ae any incoming "markers" and if not, skip the whole re-centering and zooming command. PELASE HELP!
My problem lies with the last line of each of the following two functions. Here is the code (slightly simplified from the Google original):
function load() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById('map'));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(40, -100), 3);
showMap();
}
}
function showMap() {
var searchUrl = 'map_points.xml';
GDownloadUrl(searchUrl, function(data) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName('marker');
map.clearOverlays();
var bounds = new GLatLngBounds();
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute('name');
var address = markers[i].getAttribute('address');
var distance = parseFloat(markers[i].getAttribute('distance'));
var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),parseFloat(markers[i].getAttribute('lng')));
var marker = createMarker(point, name, address, distance);
map.addOverlay(marker);
bounds.extend(point);
}
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
});
}