<?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>iPhone Programming Tutorials &#187; MKReverseGeocoder</title>
	<atom:link href="/tag/mkreversegeocoder/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Tue, 19 Nov 2013 19:34:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<item>
		<title>Adding Local Weather Conditions To Your App (Part 2/2: Accessing Google&#8217;s XML Weather API)</title>
		<link>http://icodeblog.com/2010/09/29/adding-local-weather-conditions-to-your-app-part-22-accessing-googles-xml-weather-api/</link>
		<comments>http://icodeblog.com/2010/09/29/adding-local-weather-conditions-to-your-app-part-22-accessing-googles-xml-weather-api/#comments</comments>
		<pubDate>Wed, 29 Sep 2010 19:36:36 +0000</pubDate>
		<dc:creator><![CDATA[Matt Tuzzolo]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[CoreLocation]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[MKReverseGeocoder]]></category>
		<category><![CDATA[TouchXML]]></category>
		<category><![CDATA[Weather]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2405</guid>
		<description><![CDATA[<a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-12.41.17-PM1.png"></a>
In this Part 2 of &#8216;Adding Local Weather Conditions To Your App&#8217;, I&#8217;ll show you how to quickly add current temp, conditions, and today&#8217;s high / low temperature to your app.
If you&#8217;re lucky enough to already have the user&#8217;s zipcode or city and state, this should go very quickly for you.  Otherwise, check out <a href="/2010/09/03/adding-local-weather-conditions-to-your-app-part-12-implementing-corelocation/">Part 1 (Integrating CoreLocation)</a>.
Let&#8217;s get started.
There are a handful of solid XML Weather APIs out there.  The best one I&#8217;ve seen so  ...]]></description>
				<content:encoded><![CDATA[<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-12.41.17-PM1.png"><img src="/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-12.41.17-PM1.png" alt="" title="Screen shot 2010-09-29 at 12.41.17 PM" width="378" height="210" class="aligncenter size-full wp-image-2433" /></a><br />
In this Part 2 of &#8216;Adding Local Weather Conditions To Your App&#8217;, I&#8217;ll show you how to quickly add current temp, conditions, and today&#8217;s high / low temperature to your app.</p>
<p>If you&#8217;re lucky enough to already have the user&#8217;s zipcode or city and state, this should go very quickly for you.  Otherwise, check out <a href="/2010/09/03/adding-local-weather-conditions-to-your-app-part-12-implementing-corelocation/">Part 1 (Integrating CoreLocation)</a>.</p>
<p>Let&#8217;s get started.</p>
<p>There are a handful of solid XML Weather APIs out there.  The best one I&#8217;ve seen so far is Wunderground&#8217;s (it&#8217;s extremely well documented) but for the purposes of this tutorial, I decided to use Google&#8217;s &#8220;super secret&#8221; Weather API.  It&#8217;s incredibly simple and should take care of all your basic weather needs.  Though if you&#8217;re planning on releasing a production App, be sure to pick a public API and check out their TOS (some require API keys, or fees for production use).  Here&#8217;s a good list of <a href="http://www.programmableweb.com/apitag/weather">Weather APIs</a></p>
<p>Let&#8217;s look at some example calls to Google:</p>
<p>http://www.google.com/ig/api?weather=01451</p>
<p>http://www.google.com/ig/api?weather=nyc</p>
<p>http://www.google.com/ig/api?weather=Portland,OR</p>
<p>http://www.google.com/ig/api?weather=Jamaica</p>
<p>As you can see, there&#8217;s a bit of flexibility in how you can query the service.  The one piece it&#8217;s lacking is querying by latitude and longitude.  Lucky for you, I&#8217;ll show you how to use MKReverseGeocoder to determine your user&#8217;s City/State, which you can then plug right into the GET request.  First, let&#8217;s take a quick look at the XML that comes back from the API:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">&lt;?xml version<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;1.0&quot;</span>?&gt;
  &lt;xml_api_reply version<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;1&quot;</span>&gt;
    &lt;weather module_id<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;0&quot;</span> tab_id<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;0&quot;</span> mobile_row<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;0&quot;</span> mobile_zipped<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;1&quot;</span> row<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;0&quot;</span> section<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;0&quot;</span>&gt;
      &lt;forecast_information&gt;
        &lt;city data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Portland, OR&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;postal_code data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;97217&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;latitude_e6 data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;longitude_e6 data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;forecast_date data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;2010-09-20&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;current_date_time data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;2010-09-20 23:46:50 +0000&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;unit_system data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;US&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;<span style="color: #002200;">/</span>forecast_information&gt;
      &lt;current_conditions&gt;
        &lt;condition data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Cloudy&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;temp_f data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;64&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;temp_c data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;18&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;humidity data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Humidity: 55%&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;icon data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;/ig/images/weather/cloudy.gif&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;wind_condition data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Wind: SW at 8 mph&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;<span style="color: #002200;">/</span>current_conditions&gt;
      &lt;forecast_conditions&gt;
        &lt;day_of_week data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Mon&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;low data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;51&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;high data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;66&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;icon data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;/ig/images/weather/partly_cloudy.gif&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;condition data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Partly Cloudy&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;<span style="color: #002200;">/</span>forecast_conditions&gt;
      &lt;forecast_conditions&gt;
        &lt;day_of_week data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Tue&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;low data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;50&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;high data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;68&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;icon data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;/ig/images/weather/partly_cloudy.gif&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;condition data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Partly Cloudy&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;<span style="color: #002200;">/</span>forecast_conditions&gt;
      &lt;forecast_conditions&gt;
        &lt;day_of_week data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Wed&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;low data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;53&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;high data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;68&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;icon data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;/ig/images/weather/sunny.gif&quot;</span><span style="color: #002200;">/</span>&gt;
        &lt;condition data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Sunny&quot;</span><span style="color: #002200;">/</span>&gt;
     &lt;<span style="color: #002200;">/</span>forecast_conditions&gt;
    &lt;forecast_conditions&gt;
      &lt;day_of_week data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Thu&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;low data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;53&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;high data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;65&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;icon data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;/ig/images/weather/rain.gif&quot;</span><span style="color: #002200;">/</span>&gt;
      &lt;condition data<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;Showers&quot;</span><span style="color: #002200;">/</span>&gt;
    &lt;<span style="color: #002200;">/</span>forecast_conditions&gt;
  &lt;<span style="color: #002200;">/</span>weather&gt;
&lt;<span style="color: #002200;">/</span>xml_api_reply&gt;</pre></td></tr></table></div>

<p>All this should be pretty self explanatory.  The two pieces to pay attention to here are current_conditions, and forecast_conditions.  For our demo app, we&#8217;re simply going to display current temperature, conditions, a conditionsIcon, and today&#8217;s high and low temp.  We&#8217;ll be able to pull all this information out of current_conditions and the first forecast_conditions (which is the forecast for today).  In the interest of keeping everything organized, let&#8217;s build a class to hold our weather info.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  ICB_WeatherConditions.h</span>
<span style="color: #11740a; font-style: italic;">//  LocalWeather</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt Tuzzolo on 9/28/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2010 iCodeBlog. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #a61390;">@interface</span> ICB_WeatherConditions <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>condition, <span style="color: #002200;">*</span>location;
    <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>conditionImageURL;
    NSInteger currentTemp,lowTemp,highTemp;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>condition, <span style="color: #002200;">*</span>location;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>conditionImageURL;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic<span style="color: #002200;">&#41;</span> NSInteger currentTemp, lowTemp, highTemp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>ICB_WeatherConditions <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>initWithQuery<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>query;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>In the .m we&#8217;re going to pull the data out of the XML and store it in our properties.  There are several 3rd party Objective-C XML parsers.  I&#8217;ve chosen to use Jonathan Wight&#8217;s TouchXML as it&#8217;s become somewhat of a standard for parsing XML on iOS.  You can find it <a href="http://github.com/schwa/TouchXML">here</a>.  You&#8217;ll have to jump through a couple hoops to get TouchXML into your project.  Here&#8217;s an excellent <a href="http://foobarpig.com/iphone/touchxml-installation-guide.html">tutorial on installing TouchXML</a> that will walk you through the whole process if you&#8217;ve never done it before.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  ICB_WeatherConditions.m</span>
<span style="color: #11740a; font-style: italic;">//  LocalWeather</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt Tuzzolo on 9/28/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2010 iCodeBlog. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;ICB_WeatherConditions.h&quot;</span>
<span style="color: #6e371a;">#import &quot;TouchXML.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> ICB_WeatherConditions
&nbsp;
<span style="color: #a61390;">@synthesize</span> currentTemp, condition, conditionImageURL, location, lowTemp, highTemp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>ICB_WeatherConditions <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>initWithQuery<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>query
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        CXMLDocument <span style="color: #002200;">*</span>parser <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CXMLDocument alloc<span style="color: #002200;">&#93;</span> initWithContentsOfURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.google.com/ig/api?weather=%@&quot;</span>, query<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> options<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
        condition         <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>parser nodesForXPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/xml_api_reply/weather/current_conditions/condition&quot;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> attributeForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
        location          <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>parser nodesForXPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/xml_api_reply/weather/forecast_information/city&quot;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> attributeForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
&nbsp;
        currentTemp       <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>parser nodesForXPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/xml_api_reply/weather/current_conditions/temp_f&quot;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> attributeForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span> integerValue<span style="color: #002200;">&#93;</span>;
        lowTemp           <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>parser nodesForXPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/xml_api_reply/weather/forecast_conditions/low&quot;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> attributeForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span> integerValue<span style="color: #002200;">&#93;</span>;
        highTemp          <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>parser nodesForXPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/xml_api_reply/weather/forecast_conditions/high&quot;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> attributeForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span> integerValue<span style="color: #002200;">&#93;</span>;
&nbsp;
        conditionImageURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.google.com%@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>parser nodesForXPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;/xml_api_reply/weather/current_conditions/icon&quot;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> attributeForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>conditionImageURL release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>condition release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>location release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>I&#8217;ve decided to write my own init method to handle making the request to our API.  This will make for a clean implementation in our view controller.</p>
<p>Before we get to implementing ICB_WeatherConditions, I&#8217;ll touch briefly on location.  Part 1/2 of this tutorial covered finding your user&#8217;s latitude and longitude with Core Location.  Use MKReverseGeocoder to find city/state from coordinates.  Start by adding both the MapKit and CoreLocation frameworks to your project.</p>
<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-11.42.59-AM1.png"><img src="/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-11.42.59-AM1.png" alt="" title="Screen shot 2010-09-29 at 11.42.59 AM" width="458" height="336" class="alignnone size-full wp-image-2416" /></a></p>
<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-11.43.12-AM1.png"><img src="/wp-content/uploads/2010/09/Screen-shot-2010-09-29-at-11.43.12-AM1.png" alt="" title="Screen shot 2010-09-29 at 11.43.12 AM" width="358" height="548" class="alignnone size-full wp-image-2415" /></a></p>
<p>MKReverseGeocoder works asynchronously to resolve location info; it has a delegate.  In our example we set the delegate to the view controller (self).  Be sure to add <mkreverseGeocoderDelegate> to your header as well.  Since your view controller is now the delegate for the geocoder, make sure to implement the delegate methods for the MKReverseGeocoderDelegate protocol:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#pragma mark MKReverseGeocoder Delegate Methods</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>reverseGeocoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKReverseGeocoder <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>geocoder didFindPlacemark<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKPlacemark <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>placemark
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>geocoder release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self performSelectorInBackground<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>showWeatherFor<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>placemark.addressDictionary objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ZIP&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>reverseGeocoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKReverseGeocoder <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>geocoder didFailWithError<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>error
<span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;reverseGeocoder:%@ didFailWithError:%@&quot;</span>, geocoder, error<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>geocoder release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Now we&#8217;re ready to implement ICB_WeatherConditions.  I usually populate UILabels in viewDidLoad, but since we&#8217;re making API calls and downloading a remote image (the weather conditions icon), I decided to write a method to execute in the background.  This lets us use synchronous requests (which a lot easier to deal with) to handle network requests without locking up the main thread.  Once our network calls have finished, we call back to the main thread to update the UI accordingly.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// This will run in the background</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>showWeatherFor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>query
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span>pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
    ICB_WeatherConditions <span style="color: #002200;">*</span>weather <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ICB_WeatherConditions alloc<span style="color: #002200;">&#93;</span> initWithQuery<span style="color: #002200;">:</span>query<span style="color: #002200;">&#93;</span>;
&nbsp;
    self.conditionsImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImage imageWithData<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithContentsOfURL<span style="color: #002200;">:</span>weather.conditionImageURL<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>self performSelectorOnMainThread<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>updateUI<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>weather waitUntilDone<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>pool release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// This happens in the main thread</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>updateUI<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ICB_WeatherConditions <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>weather
<span style="color: #002200;">&#123;</span>
    self.conditionsImageView.image <span style="color: #002200;">=</span> self.conditionsImage;
    <span style="color: #002200;">&#91;</span>self.conditionsImage release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>self.currentTempLabel setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d&quot;</span>, weather.currentTemp<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.highTempLabel setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d&quot;</span>, weather.highTemp<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.lowTempLabel setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d&quot;</span>, weather.lowTemp<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.conditionsLabel setText<span style="color: #002200;">:</span>weather.condition<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.cityLabel setText<span style="color: #002200;">:</span>weather.location<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>weather release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Of course make sure your NIB is connected to your IBOutlets properly.</p>
<p>Now the final piece:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self performSelectorInBackground<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>showWeatherFor<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;97217&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Build and Run:</p>
<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-28-at-5.08.44-PM1.png"><img src="/wp-content/uploads/2010/09/Screen-shot-2010-09-28-at-5.08.44-PM1.png" alt="" title="Final" width="416" height="735" class="alignnone size-full wp-image-2417" /></a></p>
<p>And you&#8217;re done!</p>
<p>You can see the complete class below.  I&#8217;ve also posted a <a href="http://github.com/elc/ICB_LocalWeather">demo project</a> to github.</p>
<p>My name is Matt Tuzzolo (<a href="http://twitter.com/matt_tuzzolo">@matt_tuzzolo</a>).  I hope you found this post helpful.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  LocalWeatherViewController.h</span>
<span style="color: #11740a; font-style: italic;">//  LocalWeather</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt Tuzzolo on 8/30/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright iCodeBlog LLC 2010. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &lt;uikit/UIKit.h&gt;</span>
<span style="color: #6e371a;">#import &quot;MapKit/MapKit.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> LocalWeatherViewController <span style="color: #002200;">:</span> UIViewController &lt;mkreverseGeocoderDelegate&gt; <span style="color: #002200;">&#123;</span>
    IBOutlet UILabel <span style="color: #002200;">*</span>currentTempLabel, <span style="color: #002200;">*</span>highTempLabel, <span style="color: #002200;">*</span>lowTempLabel, <span style="color: #002200;">*</span>conditionsLabel, <span style="color: #002200;">*</span>cityLabel;
    IBOutlet UIImageView <span style="color: #002200;">*</span>conditionsImageView;
    UIImage <span style="color: #002200;">*</span>conditionsImage;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> IBOutlet UILabel <span style="color: #002200;">*</span>currentTempLabel, <span style="color: #002200;">*</span>highTempLabel, <span style="color: #002200;">*</span>lowTempLabel, <span style="color: #002200;">*</span>conditionsLabel, <span style="color: #002200;">*</span>cityLabel;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> IBOutlet UIImageView <span style="color: #002200;">*</span>conditionsImageView;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> UIImage <span style="color: #002200;">*</span>conditionsImage;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>updateUI<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ICB_WeatherConditions <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>weather;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>And the .m:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  LocalWeatherViewController.m</span>
<span style="color: #11740a; font-style: italic;">//  LocalWeather</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt Tuzzolo on 8/30/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright iCodeBlog LLC 2010. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;LocalWeatherViewController.h&quot;</span>
<span style="color: #6e371a;">#import &quot;ICB_WeatherConditions.h&quot;</span>
<span style="color: #6e371a;">#import &quot;MapKit/MapKit.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> LocalWeatherViewController
&nbsp;
<span style="color: #a61390;">@synthesize</span> currentTempLabel, highTempLabel, lowTempLabel, conditionsLabel, cityLabel;
<span style="color: #a61390;">@synthesize</span> conditionsImageView;
<span style="color: #a61390;">@synthesize</span> conditionsImage;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span> <span style="color: #11740a; font-style: italic;">//you have coordinates but need a city</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Check out Part 1 of the tutorial to see how to find your Location with CoreLocation</span>
        CLLocationCoordinate2D coord;
        coord.latitude <span style="color: #002200;">=</span> <span style="color: #2400d9;">45.574779</span>;
        coord.longitude <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">122.685366</span>;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// Geocode coordinate (normally we'd use location.coordinate here instead of coord).</span>
        <span style="color: #11740a; font-style: italic;">// This will get us something we can query Google's Weather API with</span>
        MKReverseGeocoder <span style="color: #002200;">*</span>geocoder <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MKReverseGeocoder alloc<span style="color: #002200;">&#93;</span> initWithCoordinate<span style="color: #002200;">:</span>coord<span style="color: #002200;">&#93;</span>;
        geocoder.delegate <span style="color: #002200;">=</span> self;
        <span style="color: #002200;">&#91;</span>geocoder start<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">else</span> <span style="color: #11740a; font-style: italic;">// You already know your users zipcode, city, or otherwise.</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Do this in the background so we don't lock up the UI.</span>
        <span style="color: #002200;">&#91;</span>self performSelectorInBackground<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>showWeatherFor<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;97217&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>showWeatherFor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>query
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSAutoreleasePool</span> <span style="color: #002200;">*</span>pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAutoreleasePool</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
    ICB_WeatherConditions <span style="color: #002200;">*</span>weather <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ICB_WeatherConditions alloc<span style="color: #002200;">&#93;</span> initWithQuery<span style="color: #002200;">:</span>query<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>self.currentTempLabel setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d&quot;</span>, weather.currentTemp<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.highTempLabel setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d&quot;</span>, weather.highTemp<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.lowTempLabel setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d&quot;</span>, weather.lowTemp<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.conditionsLabel setText<span style="color: #002200;">:</span>weather.condition<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.cityLabel setText<span style="color: #002200;">:</span>weather.location<span style="color: #002200;">&#93;</span>;
&nbsp;
    self.conditionsImageView.image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithData<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithContentsOfURL<span style="color: #002200;">:</span>weather.conditionImageURL<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>weather release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>pool release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark MKReverseGeocoder Delegate Methods</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>reverseGeocoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKReverseGeocoder <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>geocoder didFindPlacemark<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKPlacemark <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>placemark
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>geocoder release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self performSelectorInBackground<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>showWeatherFor<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>placemark.addressDictionary objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ZIP&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>reverseGeocoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKReverseGeocoder <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>geocoder didFailWithError<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>error
<span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;reverseGeocoder:%@ didFailWithError:%@&quot;</span>, geocoder, error<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>geocoder release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>didReceiveMemoryWarning <span style="color: #002200;">&#123;</span>
     <span style="color: #002200;">&#91;</span>super didReceiveMemoryWarning<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidUnload <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Release any retained subviews of the main view.</span>
	<span style="color: #11740a; font-style: italic;">// e.g. self.myOutlet = nil;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/09/29/adding-local-weather-conditions-to-your-app-part-22-accessing-googles-xml-weather-api/feed/</wfw:commentRss>
		<slash:comments>40</slash:comments>
		</item>
		<item>
		<title>Introduction to MapKit in iPhone OS 3.0 Part 2</title>
		<link>http://icodeblog.com/2009/12/22/introduction-to-mapkit-in-iphone-os-3-0-part-2/</link>
		<comments>http://icodeblog.com/2009/12/22/introduction-to-mapkit-in-iphone-os-3-0-part-2/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 18:53:56 +0000</pubDate>
		<dc:creator><![CDATA[Collin]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[MapKit]]></category>
		<category><![CDATA[MKMapView]]></category>
		<category><![CDATA[MKReverseGeocoder]]></category>
		<category><![CDATA[MKReverseGeocoderDelegate]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1558</guid>
		<description><![CDATA[Introduction
Back in September I <a title="Introduction to MapKit in iPhone OS 3.0" href="/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/" target="_blank">posted a large post</a> going over all the components required to implement the MapKit in your application. The MapKit is a framework introduced in iPhone OS 3.0 and allows developers to easily take advantage of Google&#8217;s mapping technology. In my first post I go over how to present a map as well as annotate the map with custom badges to highlight points of interest. The MapKit also  ...]]></description>
				<content:encoded><![CDATA[<h1><span style="color: #ff6600;">Introduction</span></h1>
<p><span style="color: #000000;">Back in September I <a title="Introduction to MapKit in iPhone OS 3.0" href="/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/" target="_blank">posted a large post</a> going over all the components required to implement the MapKit in your application. The MapKit is a framework introduced in iPhone OS 3.0 and allows developers to easily take advantage of Google&#8217;s mapping technology. In my first post I go over how to present a map as well as annotate the map with custom badges to highlight points of interest. The MapKit also gives developers access to reverse geocoding services from Google which I will cover in this post.</span></p>
<h1><span style="color: #ff6600;">Reverse Geocoding</span></h1>
<p><span style="color: #000000;">A users location is defined by coordinates. When using the Core Location services, or specifying where a map should center its view, coordinates will be the units developers will be working with. This is all well and good for presenting mapping information visually but there is a whole other set of information that can be derived from a set of coordinates.</span></p>
<ul>
<li>Country</li>
<li>State</li>
<li>Zip Code</li>
<li>Address</li>
</ul>
<p>Google provides the service to translate any coordinate set to an MKPlacemark object. An MKPlacemark object contains properties to access all this information. Let&#8217;s look into the process of getting this object.</p>
<h2>Step 1</h2>
<p>The first thing you need to do is make a &#8220;View Based Application&#8221;. Now we need to is pick a set of coordinates we want to use. I have decided to use the address of Arizona State University.</p>
<p><strong>Longitude: -111.936619;</strong></p>
<p><strong>Latitude: 33.416199;</strong></p>
<p>Make your AppDelegates .m file look like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Override point for customization after app launch</span>
    <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>viewController.view<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
	CLLocationCoordinate2D coord;
	coord.longitude <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">111.936619</span>;
	coord.latitude <span style="color: #002200;">=</span> <span style="color: #2400d9;">33.416199</span>;
&nbsp;
	MKReverseGeocoder <span style="color: #002200;">*</span>geocoder <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MKReverseGeocoder alloc<span style="color: #002200;">&#93;</span> initWithCoordinate<span style="color: #002200;">:</span>coord<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>geocoder setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>geocoder start<span style="color: #002200;">&#93;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<h2>Step 2</h2>
<p>Now we need to implement the MKReverseGeocoderDelegate methods. There are only 2 delegate methods:</p>
<p>Called when an error occurs when fetching the reverse geocoder object</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>reverseGeocoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKReverseGeocoder <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>geocoder didFailWithError<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>error</pre></td></tr></table></div>

<p>Called when the ReverseGeocode object is successfully returned.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>reverseGeocoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKReverseGeocoder <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>geocoder didFindPlacemark<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKPlacemark <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>placemark</pre></td></tr></table></div>

<h2>Step 3</h2>
<p>Now we have a MKPlacemark object to use. An MKPlacemark object conforms to the MKAnnotation protocol. So these objects could be added as annotations to an MKMapView. For more information on MKAnnotations reference <a title="Introduction to MapKit in iPhone OS 3.0" href="/2009/12/21/introduction-to-mapkit-in-iphone-os-3-0/">part 1</a>. The MKPlacemark object contains the following properties that you can access to use in your application.</p>
<p><a href="/wp-content/uploads/2009/12/Screen-shot-2009-12-22-at-11.48.05-AM1.png"><img class="aligncenter size-full wp-image-1563" title="Screen shot 2009-12-22 at 11.48.05 AM" src="/wp-content/uploads/2009/12/Screen-shot-2009-12-22-at-11.48.05-AM1.png" alt="" width="597" height="400" /></a></p>
<p>To see all of these values make the success delegate method look like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>reverseGeocoder<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKReverseGeocoder <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>geocoder didFindPlacemark<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MKPlacemark <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>placemark
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;The geocoder has returned: %@&quot;</span>, <span style="color: #002200;">&#91;</span>placemark addressDictionary<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>You should see this output:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #2400d9;">2009</span><span style="color: #002200;">-</span><span style="color: #2400d9;">12</span><span style="color: #002200;">-</span><span style="color: #2400d9;">22</span> <span style="color: #2400d9;">11</span><span style="color: #002200;">:</span><span style="color: #2400d9;">23</span><span style="color: #002200;">:</span><span style="color: #2400d9;">05.492</span> ReverseGeocoder<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">2591</span><span style="color: #002200;">:</span><span style="color: #2400d9;">207</span><span style="color: #002200;">&#93;</span> The geocoder has returned<span style="color: #002200;">:</span> <span style="color: #002200;">&#123;</span>
    City <span style="color: #002200;">=</span> Tempe;
    Country <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;United States&quot;</span>;
    CountryCode <span style="color: #002200;">=</span> US;
    FormattedAddressLines <span style="color: #002200;">=</span>     <span style="color: #002200;">&#40;</span>
        <span style="color: #bf1d1a;">&quot;200-206 E Lemon St&quot;</span>,
        <span style="color: #bf1d1a;">&quot;Tempe, AZ 85281&quot;</span>,
        USA
    <span style="color: #002200;">&#41;</span>;
    State <span style="color: #002200;">=</span> Arizona;
    Street <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;200-206 E Lemon St&quot;</span>;
    SubAdministrativeArea <span style="color: #002200;">=</span> Maricopa;
    SubThoroughfare <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;200-206&quot;</span>;
    Thoroughfare <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;E Lemon St&quot;</span>;
    ZIP <span style="color: #002200;">=</span> <span style="color: #2400d9;">85281</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/12/22/introduction-to-mapkit-in-iphone-os-3-0-part-2/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
