After a button is clicked, addresses (which are stored in a PHP array) are displayed on an embedded Google Map on my site. I would like the embedded map to automatically zoom to fit the markers. How would I go about this? Here's the important part of my code (I can post the entire PHP file if necessary):
<script type="text/javascript">
var map = null;
var geocoder = null;
var latlngbounds = new GLatLngBounds( );
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
<?php if(!isset($_POST['viewed'])){
echo 'map.setCenter(new GLatLng(36.879621, -95.537109), 2);';}
else
{
echo 'function fitMap( GMap2, points ) {
var bounds = new GLatLngBounds();
for (var i=0; i< points.length; i++) {
bounds.extend(points[i]);
}
map.setZoom(map.getBoundsZoomLevel(bounds));
map.setCenter(bounds.getCenter());
}';
fitMap();
}
?>
map.addControl(new GLargeMapControl());
geocoder = new GClientGeocoder();
}
}
I'm trying to use this guy's code, but I can't implement it right:
function fitMap( map, points ) {
var bounds = new GLatLngBounds();
for (var i=0; i< points.length; i++) {
bounds.extend(points[i]);
}
map.setZoom(map.getBoundsZoomLevel(bounds));
map.setCenter(bounds.getCenter());
}
From
http://blog.jeromeparadis.com/archive/2007/02/08/1468.aspxI'm fairly well-versed with PHP, but Java is very new to me. Sorry for the long post, but I'd appreciate any help.
[EDIT: SOLVED]
I fixed this using this code:
http://econym.org.uk/gmap/basic14.htmI also changed
var latlngbounds = new GLatLngBounds( );
to
var bounds = new GLatLngBounds( );