Jump to content

google maps zoom to bounds


laPistola

Recommended Posts

This script worked fine right up to the point i added the lines marked with comments // This

 

The script is:

<script type="text/javascript">
function initialize() {
if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("map_canvas"));
	map.setCenter(new GLatLng(53.4989705,-2.1118907), 13);
	var bounds = new GLatLngBounds();
	function zoomfit() { // This Function
		newzoom = getBoundsZoomLevel(bounds);
		newcenter = bounds.getCenter();
		map.setCenter (newcenter,newzoom);
	}
	var location = new GLatLng(53.4989705,-2.1118907);
	var marker = new GMarker(location);
	marker.value = 1;
	GEvent.addListener(marker, "click", function() {
	var myHtml = "<b>#" + "1" + "</b><br/>" + "Your search location";
	map.openInfoWindowHtml(location, myHtml);
	});
	map.addOverlay(marker);
	map.addOverlay(createMarker(new GLatLng(53.5390200,-2.1297100),2,"Booth House"));
	map.addOverlay(createMarker(new GLatLng(53.7462530,-1.5942410),3,"City Mills"));
	function createMarker(point, number, message) {
		var marker = new GMarker(point);
		marker.value = number;
		GEvent.addListener(marker, "click", function() {
		var myHtml = "<b>#" + number + "</b><br/>" + message;
		map.openInfoWindowHtml(point, myHtml);
		});
		bounds.extend(point); // This
		zoomfit(); // This
		return marker;
	}
	map.setUIToDefault();
}
}
</script>

 

When the pages loads the map loads but without the controls, only showing the one marker, as well as the status displays error on page.

 

When i remove

bounds.extend(point); // This
zoomfit(); // This

 

The script works fine again with no errors

 

Any ideas on how to can  get this working?

 

Thank you in advanced

Link to comment
Share on other sites

Thank you but im still confused to what your getting at sorry.

 

The return marker; works fine i only get errors when i add the lines below

 

bounds.extend(point);
zoomfit();

 

from what you said i gathered i had to move the above code from that function, this is what i have now but still i have no controls on the map nor is it showing the 2 markers

 

<script type="text/javascript">
function initialize() {
if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("map_canvas"));
	map.setCenter(new GLatLng(53.4989705,-2.1118907), 13);
	var bounds = new GLatLngBounds();
	function zoomfit() {
		newzoom = getBoundsZoomLevel(bounds);
		newcenter = bounds.getCenter();
		map.setCenter (newcenter,newzoom);
	}
	var location = new GLatLng(53.4989705,-2.1118907);
	var marker = new GMarker(location);
	marker.value = 1;
	GEvent.addListener(marker, "click", function() {
	var myHtml = "<b>#" + "1" + "</b><br/>" + "Your search location";
	map.openInfoWindowHtml(location, myHtml);
	});
	map.addOverlay(marker);
	var location2 = new GLatLng(53.5390200,-2.1297100);
	map.addOverlay(createMarker(location2,2,"Booth House "));
	bounds.extend(location2);
	zoomfit();
	map.addOverlay(createMarker(new GLatLng(53.7462530,-1.5942410),3,"City Mills		"));
	function createMarker(point, number, message) {
		var marker = new GMarker(point);
		marker.value = number;
		GEvent.addListener(marker, "click", function() {
		var myHtml = "<b>#" + number + "</b><br/>" + message;
		map.openInfoWindowHtml(point, myHtml);
		});
		return marker;
	}
	map.setUIToDefault();
}
}
</script>

Link to comment
Share on other sites

i now have

 

<script type="text/javascript">
function initialize() {
if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("map_canvas"));
	map.setCenter(new GLatLng(53.4989705,-2.1118907), 13);
	var bounds = new GLatLngBounds();
	var location = new GLatLng(53.4989705,-2.1118907);
	var marker = new GMarker(location);
	marker.value = 1;
	GEvent.addListener(marker, "click", function() {
	var myHtml = "<b>#" + "1" + "</b><br/>" + "Your search location";
	map.openInfoWindowHtml(location, myHtml);
	});
	map.addOverlay(marker);
	bounds.extend(location);
	var location2 = new GLatLng(53.5390200,-2.1297100);
	createMarker(location2,2,"Booth House ");
		bounds.extend(location2);
	createMarker(new GLatLng(53.7462530,-1.5942410),3,"City Mills		");
	function createMarker(point, number, message) {
		var marker = new GMarker(point);
		marker.value = number;
		GEvent.addListener(marker, "click", function() {
		var myHtml = "<b>#" + number + "</b><br/>" + message;
		map.openInfoWindowHtml(point, myHtml);
		});
		map.addOverlay(marker);
	}
	map.setUIToDefault();
	zoomfit();
	function zoomfit() {
		newzoom = getBoundsZoomLevel(bounds);
		newcenter = bounds.getCenter();
		map.setCenter (newcenter,newzoom);
	}
}
}
</script>

 

which has fixed the controls but still its only zooming to the one marker??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.