I've added markers to a Google map and used map.fitBounds() to zoom the map out so that the markets sit at the edge of the map. This is working.
There are, however, some problems when just showing the one marker.
- The first issue is that it shows the maps center (which you have to provide) before it shows the location itself.
- The second issue is that it zooms in as far as possible, rather than the zoom level specified in the options. I assume this has something to do with the fitBounds().
You can see the site here:
http://198.211.123.245/tga-directory/directory/
If you click on one of the businesses it'll take you to the single page with just one marker.
Here is my code:
jQuery(document).ready(function ($) {
function initialize() {
var myOptions = {
zoom: 14,
center: new google.maps.LatLng(51.4800, 0),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map"), myOptions);
setMarkers(map, data.companies);
infowindow = new google.maps.InfoWindow({
content: "loading..."
});
}
var markerBounds = new google.maps.LatLngBounds();
function setMarkers(map, markers) {
for (var i = 0; i < data.companies.length; i++) {
var company = data.companies[i];
var LatLng = new google.maps.LatLng(company['lat'], company['long']);
var marker = new google.maps.Marker({
position: LatLng,
map: map,
title: company['name'],
html: '<div style="width:200px; height:100px;">' + company['name'] + '<br><a href="' + company['link'] + '">View</a></div>',
});
google.maps.event.addListener(marker, "click", function () {
infowindow.setContent(this.html);
infowindow.open(map, this);
});
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': company['address']
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
markerBounds.extend(results[0].geometry.location);
map.fitBounds(markerBounds);
}
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
});
Cheers.