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
https://forums.phpfreaks.com/topic/162469-google-maps-zoom-to-bounds/
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>

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??

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.