Phil_n
10-25-2010, 11:13 AM
By reading through many articles online ive managed to doctor a Google maps Api driven by a mysql database. I have it working to point where a user can enter their postcode or town and select a radius.
The javascript then refers to a php script which pulls back the results and throws them to xml. and then the api sets markers at each stockist point. The map will zoom and center on the center of all these markers fine.
I have one problem where if you enter a postcode/town and there is only 1 result it is zoomed so far in you get just a white box and the marker. The following is the script I use so if anyone can point out where to set the zoom level of there being just one result i would really appreciate it.
<script type="text/javascript">
//<![CDATA[
var map;
var geocoder;
function load() {
if (GBrowserIsCompatible()) {
geocoder = new GClientGeocoder();
map = new GMap2(document.getElementById('map'));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(54.622978,-2.592773), 5);
}
}
function searchLocations() {
var address = document.getElementById('addressInput').value + ' UK';
geocoder.getLatLng(address, function(latlng) {
if (!latlng) {
alert(address + ' not found');
} else {
searchLocationsNear(latlng);
}
});
}
function searchLocationsNear(center) {
var radius = document.getElementById('radiusSelect').value;
var searchUrl = 'phpsqlsearch_genxml.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
GDownloadUrl(searchUrl, function(data) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName('marker');
map.clearOverlays();
var sidebar = document.getElementById('sidebar');
sidebar.innerHTML = '';
if (markers.length == 0) {
sidebar.innerHTML = 'No results found.';
map.setCenter(new GLatLng(54.622978,-2.592773), 5);
return;
}
var bounds = new GLatLngBounds();
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute('name');
var address1 = markers[i].getAttribute('address1');
var address2 = markers[i].getAttribute('address2');
var town = markers[i].getAttribute('town');
var postcode = markers[i].getAttribute('postcode');
var telephone = markers[i].getAttribute('telephone');
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, address1, address2, town, postcode, telephone);
map.addOverlay(marker);
var sidebarEntry = createSidebarEntry(marker, name, town, distance);
sidebar.appendChild(sidebarEntry);
bounds.extend(point);
}
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
});
}
function createMarker(point, name, address1, address2, town, postcode, telephone) {
var marker = new GMarker(point);
var html = '<div id="map-hover"><b>' + name + '</b> <br/>'
+ address1 + '<br />'
+ address2 + '<br />'
+ town + '<br />'
+ postcode + '<br />'
+ telephone + '</div>';
GEvent.addListener(marker, 'click', function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
function createSidebarEntry(marker, name, town, distance) {
var div = document.createElement('div');
var html = '<b>' + name + '</b> (' + distance.toFixed(1) + ')<br/>' + town;
div.innerHTML = html;
div.style.cursor = 'pointer';
div.style.marginBottom = '5px';
GEvent.addDomListener(div, 'click', function() {
GEvent.trigger(marker, 'click');
});
GEvent.addDomListener(div, 'mouseover', function() {
div.style.backgroundColor = '#eee';
});
GEvent.addDomListener(div, 'mouseout', function() {
div.style.backgroundColor = '#fff';
});
return div;
}
//]]>
</script>
The javascript then refers to a php script which pulls back the results and throws them to xml. and then the api sets markers at each stockist point. The map will zoom and center on the center of all these markers fine.
I have one problem where if you enter a postcode/town and there is only 1 result it is zoomed so far in you get just a white box and the marker. The following is the script I use so if anyone can point out where to set the zoom level of there being just one result i would really appreciate it.
<script type="text/javascript">
//<![CDATA[
var map;
var geocoder;
function load() {
if (GBrowserIsCompatible()) {
geocoder = new GClientGeocoder();
map = new GMap2(document.getElementById('map'));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(54.622978,-2.592773), 5);
}
}
function searchLocations() {
var address = document.getElementById('addressInput').value + ' UK';
geocoder.getLatLng(address, function(latlng) {
if (!latlng) {
alert(address + ' not found');
} else {
searchLocationsNear(latlng);
}
});
}
function searchLocationsNear(center) {
var radius = document.getElementById('radiusSelect').value;
var searchUrl = 'phpsqlsearch_genxml.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
GDownloadUrl(searchUrl, function(data) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName('marker');
map.clearOverlays();
var sidebar = document.getElementById('sidebar');
sidebar.innerHTML = '';
if (markers.length == 0) {
sidebar.innerHTML = 'No results found.';
map.setCenter(new GLatLng(54.622978,-2.592773), 5);
return;
}
var bounds = new GLatLngBounds();
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute('name');
var address1 = markers[i].getAttribute('address1');
var address2 = markers[i].getAttribute('address2');
var town = markers[i].getAttribute('town');
var postcode = markers[i].getAttribute('postcode');
var telephone = markers[i].getAttribute('telephone');
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, address1, address2, town, postcode, telephone);
map.addOverlay(marker);
var sidebarEntry = createSidebarEntry(marker, name, town, distance);
sidebar.appendChild(sidebarEntry);
bounds.extend(point);
}
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
});
}
function createMarker(point, name, address1, address2, town, postcode, telephone) {
var marker = new GMarker(point);
var html = '<div id="map-hover"><b>' + name + '</b> <br/>'
+ address1 + '<br />'
+ address2 + '<br />'
+ town + '<br />'
+ postcode + '<br />'
+ telephone + '</div>';
GEvent.addListener(marker, 'click', function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
function createSidebarEntry(marker, name, town, distance) {
var div = document.createElement('div');
var html = '<b>' + name + '</b> (' + distance.toFixed(1) + ')<br/>' + town;
div.innerHTML = html;
div.style.cursor = 'pointer';
div.style.marginBottom = '5px';
GEvent.addDomListener(div, 'click', function() {
GEvent.trigger(marker, 'click');
});
GEvent.addDomListener(div, 'mouseover', function() {
div.style.backgroundColor = '#eee';
});
GEvent.addDomListener(div, 'mouseout', function() {
div.style.backgroundColor = '#fff';
});
return div;
}
//]]>
</script>