<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Set the Zoom Level of an MKMapView</title>
	<atom:link href="http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/feed/" rel="self" type="application/rss+xml" />
	<link>http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/</link>
	<description>Mistakes and learnings of an iPhone developer</description>
	<lastBuildDate>Fri, 09 Dec 2011 12:26:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Robert Altman</title>
		<link>http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/comment-page-1/#comment-9357</link>
		<dc:creator>Robert Altman</dc:creator>
		<pubDate>Wed, 09 Nov 2011 23:57:14 +0000</pubDate>
		<guid isPermaLink="false">http://troybrant.net/blog/?p=6#comment-9357</guid>
		<description>I believe there is quite a significant error in the original code (and all variations).  The line

    NSInteger zoomExponent = 20 - zoomLevel;

should actually be:

    NSInteger zoomExponent = 21 - zoomLevel;

I combined the setCenterCoordinate:zoomLevel:animated: code with the getZoomLevel function (see walty&#039;s post)and created / connected two buttons (Zoom+ and Zoom-).  It&#039;s fairly simple code:

	[mapView setCenterCoordinate:mapView.centerCoordinate zoomLevel:mapZoomLevel+1 ...];

and

	[mapView setCenterCoordinate:mapView.centerCoordinate zoomLevel:mapZoomLevel-1 ...];

The result was zoom buttons which jump up 2 increments (Zoom+) or 0 (Zoom -).

The value 268435456 is actually quite significant: it is the number of pixels (horizontal or vertical) at zoom level 20.  Tiles are always 256 x 256 pixels.  The value 268435456 == 256 * 2 ^20; in other words, at zoom level 20, there are 1048576 (or 2 ^ 20) tiles, each with 256 (2 ^ 8) pixels.

I&#039;m very confident in the zoomLevel calculations; I base-lined the equation by measuring MapKit tiling at its minimum magnification.  At minimum zoom, in a full-screen iPad view, MapKit shows 16 tiles, which correlates to zoom level 2. 

If you try using the erroneous code, and setting your zoom level to 2 (which should show 16 tiles), you will actually have a map at zoom level 3 (containing 64 files).

Thanks for a great writeup - it&#039;s been hard validating all of the related map math, and this (and your other mapping explanation) saved me a bunch of time in doing so.</description>
		<content:encoded><![CDATA[<p>I believe there is quite a significant error in the original code (and all variations).  The line</p>
<p>    NSInteger zoomExponent = 20 &#8211; zoomLevel;</p>
<p>should actually be:</p>
<p>    NSInteger zoomExponent = 21 &#8211; zoomLevel;</p>
<p>I combined the setCenterCoordinate:zoomLevel:animated: code with the getZoomLevel function (see walty&#8217;s post)and created / connected two buttons (Zoom+ and Zoom-).  It&#8217;s fairly simple code:</p>
<p>	[mapView setCenterCoordinate:mapView.centerCoordinate zoomLevel:mapZoomLevel+1 ...];</p>
<p>and</p>
<p>	[mapView setCenterCoordinate:mapView.centerCoordinate zoomLevel:mapZoomLevel-1 ...];</p>
<p>The result was zoom buttons which jump up 2 increments (Zoom+) or 0 (Zoom -).</p>
<p>The value 268435456 is actually quite significant: it is the number of pixels (horizontal or vertical) at zoom level 20.  Tiles are always 256 x 256 pixels.  The value 268435456 == 256 * 2 ^20; in other words, at zoom level 20, there are 1048576 (or 2 ^ 20) tiles, each with 256 (2 ^ <img src='http://troybrant.net/blog/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> pixels.</p>
<p>I&#8217;m very confident in the zoomLevel calculations; I base-lined the equation by measuring MapKit tiling at its minimum magnification.  At minimum zoom, in a full-screen iPad view, MapKit shows 16 tiles, which correlates to zoom level 2. </p>
<p>If you try using the erroneous code, and setting your zoom level to 2 (which should show 16 tiles), you will actually have a map at zoom level 3 (containing 64 files).</p>
<p>Thanks for a great writeup &#8211; it&#8217;s been hard validating all of the related map math, and this (and your other mapping explanation) saved me a bunch of time in doing so.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PostaL</title>
		<link>http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/comment-page-1/#comment-7425</link>
		<dc:creator>PostaL</dc:creator>
		<pubDate>Fri, 09 Sep 2011 08:11:22 +0000</pubDate>
		<guid isPermaLink="false">http://troybrant.net/blog/?p=6#comment-7425</guid>
		<description>Voila, not viola :P

10x for the nice code ;)</description>
		<content:encoded><![CDATA[<p>Voila, not viola <img src='http://troybrant.net/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>10x for the nice code <img src='http://troybrant.net/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jimmy Theis</title>
		<link>http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/comment-page-1/#comment-7307</link>
		<dc:creator>Jimmy Theis</dc:creator>
		<pubDate>Sun, 04 Sep 2011 19:42:30 +0000</pubDate>
		<guid isPermaLink="false">http://troybrant.net/blog/?p=6#comment-7307</guid>
		<description>Hi Troy,

I&#039;m not sure if I missed it, but there doesn&#039;t seem to be a clear set of distribution terms with your code here. Your blog has a general copyright, but your language makes this code sound like it&#039;s free for anyone to use. Is your work here Public Domain? Released under a specific Open Source License?

Thanks</description>
		<content:encoded><![CDATA[<p>Hi Troy,</p>
<p>I&#8217;m not sure if I missed it, but there doesn&#8217;t seem to be a clear set of distribution terms with your code here. Your blog has a general copyright, but your language makes this code sound like it&#8217;s free for anyone to use. Is your work here Public Domain? Released under a specific Open Source License?</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nik</title>
		<link>http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/comment-page-1/#comment-6623</link>
		<dc:creator>Nik</dc:creator>
		<pubDate>Mon, 01 Aug 2011 03:47:53 +0000</pubDate>
		<guid isPermaLink="false">http://troybrant.net/blog/?p=6#comment-6623</guid>
		<description>I found it much simpler to set the visibleRect than to set the region of an MKMapView (and more accurate results too, when using a specific zoomScale, as opposed to a zoomLevel.

- (MKMapRect)mapRectWithCentreCoordinate:(CLLocationCoordinate2D)centreCoordinate zoomScale:(double)zoomScale
{
    MKMapPoint centrePoint = MKMapPointForCoordinate(centreCoordinate);
    double scaledWidth = self.bounds.size.width / zoomScale;
    double scaledHeight = self.bounds.size.height / zoomScale;
    
    return MKMapRectMake(centrePoint.x - (scaledWidth / 2),
						 centrePoint.y - (scaledHeight / 2),
						 scaledWidth,
						 scaledHeight);
}

- (void)setCentreCoordinate:(CLLocationCoordinate2D)centreCoordinate zoomScale:(double)zoomScale animated:(BOOL)animated
{
    [self setVisibleMapRect:[self mapRectWithCentreCoordinate:centreCoordinate zoomScale:zoomScale] animated:animated];
}

and then if you want it to work for zoomLevel, as well as for zoomScale, you can reuse the same code simply by adding the following (untested):

- (void)setCentreCoordinate:(CLLocationCoordinate2D)centreCoordinate zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated
{
    // clamp large numbers to 28
    zoomLevel = MIN(zoomLevel, 28);
    
    [self setCentreCoordinate:centreCoordinate zoomScale:[self zoomScaleForZoomLevel:zoomLevel] animated:animated];
}</description>
		<content:encoded><![CDATA[<p>I found it much simpler to set the visibleRect than to set the region of an MKMapView (and more accurate results too, when using a specific zoomScale, as opposed to a zoomLevel.</p>
<p>- (MKMapRect)mapRectWithCentreCoordinate:(CLLocationCoordinate2D)centreCoordinate zoomScale:(double)zoomScale<br />
{<br />
    MKMapPoint centrePoint = MKMapPointForCoordinate(centreCoordinate);<br />
    double scaledWidth = self.bounds.size.width / zoomScale;<br />
    double scaledHeight = self.bounds.size.height / zoomScale;</p>
<p>    return MKMapRectMake(centrePoint.x &#8211; (scaledWidth / 2),<br />
						 centrePoint.y &#8211; (scaledHeight / 2),<br />
						 scaledWidth,<br />
						 scaledHeight);<br />
}</p>
<p>- (void)setCentreCoordinate:(CLLocationCoordinate2D)centreCoordinate zoomScale:(double)zoomScale animated:(BOOL)animated<br />
{<br />
    [self setVisibleMapRect:[self mapRectWithCentreCoordinate:centreCoordinate zoomScale:zoomScale] animated:animated];<br />
}</p>
<p>and then if you want it to work for zoomLevel, as well as for zoomScale, you can reuse the same code simply by adding the following (untested):</p>
<p>- (void)setCentreCoordinate:(CLLocationCoordinate2D)centreCoordinate zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated<br />
{<br />
    // clamp large numbers to 28<br />
    zoomLevel = MIN(zoomLevel, 28);</p>
<p>    [self setCentreCoordinate:centreCoordinate zoomScale:[self zoomScaleForZoomLevel:zoomLevel] animated:animated];<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raks</title>
		<link>http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/comment-page-1/#comment-6255</link>
		<dc:creator>Raks</dc:creator>
		<pubDate>Mon, 11 Jul 2011 22:05:53 +0000</pubDate>
		<guid isPermaLink="false">http://troybrant.net/blog/?p=6#comment-6255</guid>
		<description>Awsome  code but somehow I am receiving following error &quot;implicit-conversion-shortens-64-bit-to-32-bit&quot; at code &quot;return round(MERCATOR_OFFSET - MERCATOR_RADIUS * logf((1 + sinf(latitude * M_PI / 180.0)) / (1 - sinf(latitude * M_PI / 180.0))) / 2.0)&quot;...any idea why ?

Thanks,</description>
		<content:encoded><![CDATA[<p>Awsome  code but somehow I am receiving following error &#8220;implicit-conversion-shortens-64-bit-to-32-bit&#8221; at code &#8220;return round(MERCATOR_OFFSET &#8211; MERCATOR_RADIUS * logf((1 + sinf(latitude * M_PI / 180.0)) / (1 &#8211; sinf(latitude * M_PI / 180.0))) / 2.0)&#8221;&#8230;any idea why ?</p>
<p>Thanks,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Scott</title>
		<link>http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/comment-page-1/#comment-6100</link>
		<dc:creator>Kevin Scott</dc:creator>
		<pubDate>Thu, 30 Jun 2011 20:23:55 +0000</pubDate>
		<guid isPermaLink="false">http://troybrant.net/blog/?p=6#comment-6100</guid>
		<description>AWESOME! SO AWESOME! Thanks Thanks Thanks!!!

This code rocks my socks off. Also props to Walty for the getZoomLevel as well.</description>
		<content:encoded><![CDATA[<p>AWESOME! SO AWESOME! Thanks Thanks Thanks!!!</p>
<p>This code rocks my socks off. Also props to Walty for the getZoomLevel as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mega Lee</title>
		<link>http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/comment-page-1/#comment-6061</link>
		<dc:creator>Mega Lee</dc:creator>
		<pubDate>Mon, 27 Jun 2011 03:55:13 +0000</pubDate>
		<guid isPermaLink="false">http://troybrant.net/blog/?p=6#comment-6061</guid>
		<description>It is so powerful    !  Thank  you !


Where is an example to download !?

When I use this ,could it pass the AppStore!?</description>
		<content:encoded><![CDATA[<p>It is so powerful    !  Thank  you !</p>
<p>Where is an example to download !?</p>
<p>When I use this ,could it pass the AppStore!?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marco Faustinelli</title>
		<link>http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/comment-page-1/#comment-5769</link>
		<dc:creator>Marco Faustinelli</dc:creator>
		<pubDate>Mon, 30 May 2011 10:48:04 +0000</pubDate>
		<guid isPermaLink="false">http://troybrant.net/blog/?p=6#comment-5769</guid>
		<description>I enjoyed your post very much.
However I wonder how much room there is for approximation: my app is based on Google Static Maps, it will use zoomlevels 13-18 and it will always be used with geo-location in central-european countries. I&#039;ve been looking for a simple table providing me with the relation between pixelDelta and latLongDelta given zoomLevel, but I couldn&#039;t find it. My intention is to implement your routines once to calculate those relations and feed them to my app, publishing a blog page in the process.
What is your take with regards to the magnitude of error that this process will entail? Thanks in advance, and again thanks a lot for publishing this page</description>
		<content:encoded><![CDATA[<p>I enjoyed your post very much.<br />
However I wonder how much room there is for approximation: my app is based on Google Static Maps, it will use zoomlevels 13-18 and it will always be used with geo-location in central-european countries. I&#8217;ve been looking for a simple table providing me with the relation between pixelDelta and latLongDelta given zoomLevel, but I couldn&#8217;t find it. My intention is to implement your routines once to calculate those relations and feed them to my app, publishing a blog page in the process.<br />
What is your take with regards to the magnitude of error that this process will entail? Thanks in advance, and again thanks a lot for publishing this page</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John D Pope</title>
		<link>http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/comment-page-1/#comment-5492</link>
		<dc:creator>John D Pope</dc:creator>
		<pubDate>Wed, 27 Apr 2011 00:40:46 +0000</pubDate>
		<guid isPermaLink="false">http://troybrant.net/blog/?p=6#comment-5492</guid>
		<description>Merged zoomLevel changes and Adam Cohen-Rose code here https://github.com/jdp-global/MKMapViewZoom</description>
		<content:encoded><![CDATA[<p>Merged zoomLevel changes and Adam Cohen-Rose code here <a href="https://github.com/jdp-global/MKMapViewZoom" rel="nofollow">https://github.com/jdp-global/MKMapViewZoom</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ashok</title>
		<link>http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/comment-page-1/#comment-5208</link>
		<dc:creator>Ashok</dc:creator>
		<pubDate>Sun, 03 Apr 2011 02:28:28 +0000</pubDate>
		<guid isPermaLink="false">http://troybrant.net/blog/?p=6#comment-5208</guid>
		<description>Wow, thanks for this! I was struggling trying to do this myself with the setRegion: method and trying to set the latitudeDelta and longitudeDelta and wasn&#039;t getting anywhere. 

This worked on the first try!</description>
		<content:encoded><![CDATA[<p>Wow, thanks for this! I was struggling trying to do this myself with the setRegion: method and trying to set the latitudeDelta and longitudeDelta and wasn&#8217;t getting anywhere. </p>
<p>This worked on the first try!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

