<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>designing4u.de &#187; automatic zoom level</title>
	<atom:link href="http://www.designing4u.de/tag/automatic-zoom-level/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designing4u.de</link>
	<description>Yet Another Coding Blog</description>
	<lastBuildDate>Fri, 29 Jul 2011 08:11:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Setting boundaries for google map with more markers using jQuery</title>
		<link>http://www.designing4u.de/2008/05/setting-boundaries-for-google-map-with-more-markers-using-jquery/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=setting-boundaries-for-google-map-with-more-markers-using-jquery</link>
		<comments>http://www.designing4u.de/2008/05/setting-boundaries-for-google-map-with-more-markers-using-jquery/#comments</comments>
		<pubDate>Wed, 21 May 2008 16:13:24 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[automatic zoom level]]></category>
		<category><![CDATA[boundaries]]></category>
		<category><![CDATA[GLatLngBounds]]></category>
		<category><![CDATA[GMap2]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=17</guid>
		<description><![CDATA[For a project I'm currently working on I had to display more markers in one google map. No problem at all, but only if all of them are placed in one city. You can just set up default level for the city and display as many markers as you want. In my case I had [...]]]></description>
			<content:encoded><![CDATA[<p>For a project I'm currently working on I had to display more markers in one google map. No problem at all, but only if all of them are placed in one city. You can just set up default level for the city and display as many markers as you want. In my case I had to display latest markers, which were saved all over the country. One would say no problem at all, just use default zoom level for the country and display the markers. What if 10 latest markers were placed in one city though? It would look stupid if they would be displayed covering each other. Additionally we do not want to use an onload event, therefore, we will use jQuery in this example. For this kind of problem google came up with a pretty nice solution.<br />
<span id="more-17"></span><br />
if you want to see a working demo of this example, just click <a href="http://www.designing4u.de/examples/gmap/index.php" target="_blank">here</a>.</p>
<p>One of the classes available in second version of google maps is GLatLngBounds. Instantiating this class constructs a rectangle in geographical lengths. One of the methods, called 'extend' allows us to extend this rectangle to get a perfect zoom level. It means that every time our loop will display a new marker, we will call 'extend' method, which will additionally extend the boundaries of the rectangle, so it contains that given point. An additional functionality, which will be covered by this function is an event listener, which will display info window when a user clicks on one of the markers. At the end we will set the zoom level using one of the methods (getBoundsZoomLevel()) from another class, which can be called though, without instantiating it and use the getCenter() method from GLatLngBounds to get the center of our map. </p>
<p>To save us some time, I decided to skip the part where we are creating our XML document, which contains all necessary information (latitude, longitude and text for info window) to display our markers, therefore if you need that part to be covered as well, just let me know. Let's take a closer look at our code:<br />
[source:javascript]<br />
$(document).ready(function() {<br />
	//our map container and instance of GLatLngBounds<br />
	var mapContainer = $("#map")[0];<br />
	var bounds = new GLatLngBounds();</p>
<p>	//if browser is compatible<br />
	if (GBrowserIsCompatible()) {<br />
		//if div with given id was found<br />
		if(mapContainer) {<br />
			//instance of the GMap2 and some controls<br />
			var map = new GMap2(mapContainer);<br />
			map.addControl(new GLargeMapControl);<br />
			map.addControl(new GMapTypeControl);<br />
			map.addControl(new GScaleControl);<br />
			map.setCenter(new GLatLng(0,0),0);<br />
		}<br />
		//AJAX request<br />
		$.get('coordinates.xml',function(data) {<br />
			//we are looking for all marker and looping through them<br />
			$(data).find('marker').each(function(){<br />
				//serializing some data from the request<br />
				var lat    = $(this).attr('lat');<br />
				var lng    = $(this).attr('lng');<br />
				var desc   = $(this).attr('desc');<br />
				var point  = new GLatLng(lat,lng);<br />
				var marker = new GMarker(point);</p>
<p>				//new marker<br />
				map.addOverlay(marker);<br />
				//extending the boundaries<br />
				bounds.extend(point);<br />
				//and the info window<br />
				GEvent.addListener(marker, "click", function() {<br />
					marker.openInfoWindowHtml(desc);<br />
				});</p>
<p>			});<br />
			//setting the zoom level and the center<br />
			map.setZoom(map.getBoundsZoomLevel(bounds));<br />
			map.setCenter(bounds.getCenter());<br />
		});</p>
<p>	}</p>
<p>});<br />
[/source]<br />
I hope everything is clear. In case of any question just use the comment or contact form. One more thing left to show. Here is the content of xml file:<br />
[source:xml]<br />
<?xml version="1.0" encoding="iso-8859-1"?><br />
<markers><br />
	<marker lat="52.491783" lng="13.420009" desc="Pretty street" /><br />
	<marker lat="52.496375" lng="13.442661" desc="Good Food" /><br />
	<marker lat="52.514122" lng="13.448429" desc="Nice Bar" /><br />
	<marker lat="52.437259" lng="13.328170" desc="Stadion" /><br />
</markers><br />
[/source]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/setting-boundaries-for-google-map-with-more-markers-using-jquery/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

