<?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; CoreLocation</title>
	<atom:link href="/tag/corelocation/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>How to Add GPS to Your iOS App &#8211; Part 1</title>
		<link>http://icodeblog.com/2012/06/04/how-to-add-gps-to-your-ios-app-part-1/</link>
		<comments>http://icodeblog.com/2012/06/04/how-to-add-gps-to-your-ios-app-part-1/#comments</comments>
		<pubDate>Mon, 04 Jun 2012 21:33:11 +0000</pubDate>
		<dc:creator><![CDATA[Shawn Grimes]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[CoreLocation]]></category>
		<category><![CDATA[GPS]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[iphone dev]]></category>
		<category><![CDATA[iphone development]]></category>
		<category><![CDATA[iphone programming]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3690</guid>
		<description><![CDATA[
In Part 1 of this series, I will introduce you to the very basics of CoreLocation services and getting the location of the device currently being used.
About CoreLocation
The CoreLocation framework provides your app with the ability to get a device&#8217;s current location, provided the user of the device has granted your app access to that information.
Location services are provided in two major ways using this framework:

Standard Location Services- This service provides the most accurate location information using a variety of  ...]]></description>
				<content:encoded><![CDATA[<p><center><img src="http://img.skitch.com/20120604-drip3tee432eccbfhsdkrpwhbr.jpg" alt="CoreLocation Graphic" /></center></p>
<blockquote><p>In Part 1 of this series, I will introduce you to the very basics of CoreLocation services and getting the location of the device currently being used.</p></blockquote>
<h2>About CoreLocation</h2>
<p>The CoreLocation framework provides your app with the ability to get a device&#8217;s current location, provided the user of the device has granted your app access to that information.</p>
<p>Location services are provided in two major ways using this framework:</p>
<ol>
<li><strong>Standard Location Services</strong>- This service provides the most accurate location information using a variety of methods including celluar, wifi, and GPS.
<ul>
<li>Most accurate.</li>
<li>Consumes the most power and can take a longer time to acquire.</li>
<li>Can only be used when your application is in the foreground.</li>
</ul>
</li>
<li><strong>Significant Change Location Services</strong>- This service uses cellular data to determine a general idea of where the device is located.
<ul>
<li>Fastest and uses the least amount of power to determine the location.</li>
<li>Can also be used to notify your app when a significant change in the location has occurred even if your app is not in focus.</li>
</ul>
</li>
</ol>
<h2>Project Files:</h2>
<p>You can download the project files at this github repository: <a href="http://github.com/shawngrimes/Location-and-Map-Sample">http://github.com/shawngrimes/Location-and-Map-Sample</a></p>
<h2>Adding Location Frameworks</h2>
<p>The first step to adding location services to your project is to add the CoreLocation framework.</p>
<div class="thumbnail" align="center"><a href="http://skitch.com/shawnegrimes/86jyp/link-binary-with-libraries"><img src="http://img.skitch.com/20120603-d7w4x5fyueprskt33g5xet7a1n.preview.jpg" alt="Link Binary With Libraries" /></a></div>
<ol>
<li>Select your target and go to the &#8220;Build Phases&#8221; tab.</li>
<li>Click the &#8220;+&#8221; button under &#8220;Link Binary With Libraries&#8221;</li>
<li>Select the CoreLocation framework and click &#8220;Add&#8221;</li>
</ol>
<div class="thumbnail" align="center"><a href="http://skitch.com/shawnegrimes/86je5/add-core-location-framework"><img src="http://img.skitch.com/20120603-gby2hyh3mtsfax86u5ht39491r.preview.jpg" alt="Add Core Location Framework" /></a></div>
<h2>Location Manager</h2>
<p>With location services, you create an instance of CLLocationManager. There is usually one instance of this per app so I feel a good place for this is in the App Delegate and then each view can access the current location from the App Delegate.</p>
<p>To start, open your App Delegate header file:</p>
<ol>
<li>Add <code>#import &lt;CoreLocation/CoreLocation.h&gt;</code> to the top</li>
<li>Specify that your app delegate complies with the Core Location Delegate protocol by changing your <code>@interface</code> line with <code>@interface CFAAppDelegate : UIResponder &lt;UIApplicationDelegate, CLLocationManagerDelegate&gt;</code></li>
<li>Specify a CLLocationManager property by adding <code>@property (strong, nonatomic) CLLocationManager *locationManager</code> right before the <code>@end</code> statement.</li>
</ol>
<p>Your App Delegate header file should now look similar to the following:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &amp;lt;UIKit/UIKit.h&amp;gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//Add Location Framework</span>
<span style="color: #6e371a;">#import &amp;lt;CoreLocation/CoreLocation.h&amp;gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//Specify this app delegate complies with the location manager delegate</span>
<span style="color: #a61390;">@interface</span> CFAAppDelegate <span style="color: #002200;">:</span> UIResponder <span style="color: #002200;">&amp;</span>lt;UIApplicationDelegate, CLLocationManagerDelegate<span style="color: #002200;">&amp;</span>gt;
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>strong, nonatomic<span style="color: #002200;">&#41;</span> UIWindow <span style="color: #002200;">*</span>window;
&nbsp;
<span style="color: #11740a; font-style: italic;">//Add a location manager property to this app delegate</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>strong, nonatomic<span style="color: #002200;">&#41;</span> CLLocationManager <span style="color: #002200;">*</span>locationManager;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<h2>Creating The Location Manager Object</h2>
<p>Switch over to the App Delegate implementation file (.m), and we are going to create our location manager object. The first thing we should do, since we created it as a property of our app delegate, is synthesize the property so add <code>@synthesize locationManager=_locationManager;</code> under the line that reads <code>@synthesize window = _window;</code>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;CFAAppDelegate.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> CFAAppDelegate
&nbsp;
<span style="color: #a61390;">@synthesize</span> window <span style="color: #002200;">=</span> _window;
<span style="color: #a61390;">@synthesize</span> locationManager<span style="color: #002200;">=</span>_locationManager;</pre></td></tr></table></div>

<p>Now that our property is synthesized, we can create the object. I usually create it in the method <code>application didFinishLaunchingWithOptions:</code>. A Location Manager object is created similar to any other object but there are three important properties you should set after you have alloc&#8217;d/init&#8217;d.</p>
<ol>
<li><strong>.purpose</strong> &#8211; The purpose property is displayed to the user of your app when they are prompted to allow your app to use their location. It gives you a chance to explain what your app is going to do with their location information.</li>
<li><strong>.desiredAccuracy</strong> &#8211; The desired accuracy property allows you to tell the device how accurate you would like the location information to be. This should be set based on your application&#8217;s needs. Don&#8217;t set this property to <code>kCLLocationAccuracyBest</code> if you only need to know what city they are in. <em>NOTE: This is the &#8220;desired&#8221; accuracy, it is not guaranteed. The device will determine the best available information and provide it to you but you are not, however, guaranteed any level of accuracy.</em></li>
<li><strong>.distanceFilter</strong> &#8211; The distance filter property tells the location manager how far a device needs to move (horizontally vs. an altitude change) before triggering a new location event. It is measured in meters. You can set the property to <code>kCLDistanceFilterNone</code> to be notified of all events (this is also the default value).</li>
</ol>
<p>The completed creation of our location manage looks 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;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions
<span style="color: #002200;">&#123;</span>
    self.window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWindow alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// Override point for customization after application launch.</span>
    self.window.backgroundColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self.locationManager<span style="color: #002200;">==</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
        _locationManager<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CLLocationManager alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
        <span style="color: #11740a; font-style: italic;">//I'm using ARC with this project so no need to release</span>
&nbsp;
        _locationManager.delegate<span style="color: #002200;">=</span>self;
        _locationManager.purpose <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;We will try to tell you where you are if you get lost&quot;</span>;
        _locationManager.desiredAccuracy<span style="color: #002200;">=</span>kCLLocationAccuracyBest;
        _locationManager.distanceFilter<span style="color: #002200;">=</span><span style="color: #2400d9;">500</span>;
        self.locationManager<span style="color: #002200;">=</span>_locationManager;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>If you run this now, you should not notice any change in the way your application runs, you also won&#8217;t start receiving location information just yet. That comes next&#8230;</p>
<h2>Starting Location Services</h2>
<p>Now that we have our location manager configured, we want to start getting the location. To start the standard location service, you should first check to make sure that location services are enabled. This is done with a simple call to <code>[CLLocationManager locationServicesEnabled]</code> so after we set up our location manager above and before the <code>return YES;</code>, add the following code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>CLLocationManager locationServicesEnabled<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self.locationManager startUpdatingLocation<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<blockquote><p>NOTE: If location services are not enabled and you start updating location services, the user will be prompted to enable location services. This could be annoying to the end user if they have to answer &#8220;No&#8221; every time your app launches.</p></blockquote>
<p>If you run your app now, you will notice that the user is prompted to enable location services (and it will include the message from the <code>.prompt</code> property of our location manager object).</p>
<div class="thumbnail" align="center"><a href="http://skitch.com/shawnegrimes/86idb/enablelocationservicesprompt"><img src="http://img.skitch.com/20120605-ke3m4ipx1cj2p9pprstkcdcrdn.preview.jpg" alt="EnableLocationServicesPrompt" /></a></div>
<h2>Receiving Location Changes</h2>
<p>We&#8217;ve started location services, but now we need to start receiving updates to location changes. This is all done through the <code>CLLocationManagerDelegate</code> protocol. There are a number of available methods to implement for this delegate but the three most important are probably:</p>
<ol>
<li>– locationManager:didUpdateToLocation:fromLocation:</li>
<li>– locationManager:didFailWithError:</li>
<li>– locationManager:didChangeAuthorizationStatus:</li>
</ol>
<h2>– locationManager:didUpdateToLocation:fromLocation:</h2>
<p>Let&#8217;s start with receiving an update in location and how to handle that. In our App Delegate implementation file (.m), add the following method placeholder below the <code>@synthesize …</code> statements and above <code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions</code> method:</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>locationManager<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocationManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>manager didUpdateToLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newLocation fromLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>oldLocation<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>One of the first things I like to do is check the timestamp of the newLocation detected. When you first start updating location services, the location manager will receive the last known location of the device, this could be hours or days old depending on the last time location services were used. In my apps, I like to make sure that I have a recent location. This following code will only use a location that has been found in the last 15.0 seconds:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">    <span style="color: #400080;">NSDate</span><span style="color: #002200;">*</span> eventDate <span style="color: #002200;">=</span> newLocation.timestamp;
    NSTimeInterval howRecent <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eventDate timeIntervalSinceNow<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">abs</span><span style="color: #002200;">&#40;</span>howRecent<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">15.0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">//Location timestamp is within the last 15.0 seconds, let's use it!</span>
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The next check that I like to perform is to see how accurate the newLocation is. This can be done by looking at the <code>.horizontalAccuracy</code> property. This property tells you how accurate your location information is. As I stated above, you can request to have very accurate locations but you are not guaranteed it. In the illustration below, the newLocation.coordinate is the location the device thinks it is, but in truth the orange circle of the .horizontalAccuracy says that it could be anywhere in that circle.</p>
<div class="thumbnail" align="center"><a href="https://skitch.com/shawnegrimes/86kp3/horizontalaccuracy"><img src="https://img.skitch.com/20120603-enfpceaw8jgsqw1hieenx48jmj.preview.jpg" alt="horizontalAccuracy" /></a></div>
<p>Your application&#8217;s need will determine how accurate of a location you need. Again, if you just need the city the user is in, you won&#8217;t need it to be as accurate as if you were trying to find something within walking distance. In the code below, I have set my accuracy threshold to 35.0 meters. This is pretty broad but I have found that it works well indoors as well as out. Insert the following code right after the comment <code>//Location timestamp is within the last 15.0 seconds, let's use it!</code></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">        <span style="color: #11740a; font-style: italic;">//Location timestamp is within the last 15.0 seconds, let's use it!</span>
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>newLocation.horizontalAccuracy<span style="color: #002200;">&amp;</span>lt;<span style="color: #2400d9;">35.0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">//Location seems pretty accurate, let's use it!</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;latitude %+.6f, longitude %+.6f<span style="color: #2400d9;">\n</span>&quot;</span>,
                  newLocation.coordinate.latitude,
                  newLocation.coordinate.longitude<span style="color: #002200;">&#41;</span>;
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Horizontal Accuracy:%f&quot;</span>, newLocation.horizontalAccuracy<span style="color: #002200;">&#41;</span>;
&nbsp;
            <span style="color: #11740a; font-style: italic;">//Optional: turn off location services once we've gotten a good location</span>
            <span style="color: #002200;">&#91;</span>manager stopUpdatingLocation<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The code above will print the new location to the console and then turn off location services. This is an optional step but if you don&#8217;t need location services anymore, it&#8217;s a smart thing to turn them off to conserve your user&#8217;s battery.</p>
<p>This is what our complete delegate method looks like:</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>locationManager<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocationManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>manager didUpdateToLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newLocation fromLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>oldLocation<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSDate</span><span style="color: #002200;">*</span> eventDate <span style="color: #002200;">=</span> newLocation.timestamp;
    NSTimeInterval howRecent <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eventDate timeIntervalSinceNow<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">abs</span><span style="color: #002200;">&#40;</span>howRecent<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">15.0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">//Location timestamp is within the last 15.0 seconds, let's use it!</span>
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>newLocation.horizontalAccuracy<span style="color: #002200;">&amp;</span>lt;<span style="color: #2400d9;">35.0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">//Location seems pretty accurate, let's use it!</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;latitude %+.6f, longitude %+.6f<span style="color: #2400d9;">\n</span>&quot;</span>,
                  newLocation.coordinate.latitude,
                  newLocation.coordinate.longitude<span style="color: #002200;">&#41;</span>;
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Horizontal Accuracy:%f&quot;</span>, newLocation.horizontalAccuracy<span style="color: #002200;">&#41;</span>;
&nbsp;
            <span style="color: #11740a; font-style: italic;">//Optional: turn off location services once we've gotten a good location</span>
            <span style="color: #002200;">&#91;</span>manager stopUpdatingLocation<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Now we can run the project again. If you didn&#8217;t authorize the app to use location services before, go ahead and do that now. We haven&#8217;t done anything with the location at this point except print it out to the console. If you don&#8217;t know how to see the console, make sure Xcode is the active window (NOT the simulator) and press ⌘⇧C</p>
<p>If you are running the project on the simulator and not a device, you may not see any results in the console window. This is because the simulator does not have location services turned on by default. You can simulate a location by selecting the location services simulation in the console window:</p>
<div class="thumbnail" align="center"><a href="http://skitch.com/shawnegrimes/86typ/simulate-location"><img src="http://img.skitch.com/20120604-b359j7crjba8wctd1gje5kk112.preview.jpg" alt="Simulate Location" /></a></div>
<p>You can also simulate a location in the Simulator by using the Debug menu.</p>
<div class="thumbnail" align="center"><a href="https://skitch.com/shawnegrimes/86tn4/simulator-location-services"><img src="https://img.skitch.com/20120604-qxs23ikjy6qufjpwpg72fuewk9.preview.jpg" alt="Simulator Location Services" /></a></div>
<p>Once a location has been selected, you may need to restart the app to get the new location to show in the console window:</p>
<div class="thumbnail" align="center"><a href="https://skitch.com/shawnegrimes/86trw/location-in-console"><img src="https://img.skitch.com/20120604-ddt5unstdmea3i58ddxsgfgih8.preview.jpg" alt="Location In Console" /></a></div>
<p>Because we built the location manager into the App Delegate, you can access the device&#8217;s current location anywhere in your code by including the AppDelegate header file (.h) in your view controller&#8217;s implementation file (.m):</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;CFAAppDelegate.h&quot;</span></pre></td></tr></table></div>

<p>Then whenever you need to access the location, you can use the following bit of code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//Make sure location services are enabled before requesting the location</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>CLLocationManager locationServicesEnabled<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
&nbsp;
    CFAAppDelegate <span style="color: #002200;">*</span>appDelegate<span style="color: #002200;">=</span><span style="color: #002200;">&#40;</span>CFAAppDelegate <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span>.delegate;
    CLLocation <span style="color: #002200;">*</span>currentLocation<span style="color: #002200;">=</span>appDelegate.locationManager.location;
&nbsp;
    <span style="color: #11740a; font-style: italic;">//Do what you want with the location...</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<blockquote><p><strong>About: Shawn Grimes</strong><br />
Shawn is a mobile app developer for <a href="http://elctech.com/">ELC Technologies</a>. He is a co-author of the book, <a href="http://www.amazon.com/gp/product/1430240059/ref=as_li_ss_tl?ie=UTF8&amp;tag=charmtrave-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1430240059">iOS 5 Recipes: A Problem-Solution Approach (Recipes Apress)</a>. You can follow him on twitter <a href="https://twitter.com/#!/shawng">@shawng</a>. Shawn also works with high school students to teach them app development skills through the <a href="http://www.appliedclub.org">APPlied Club</a> Program.</p></blockquote>
<p>UIKit/UIKit.hp</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2012/06/04/how-to-add-gps-to-your-ios-app-part-1/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<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>Adding Local Weather Conditions To Your App (Part 1/2: Implementing CoreLocation)</title>
		<link>http://icodeblog.com/2010/09/03/adding-local-weather-conditions-to-your-app-part-12-implementing-corelocation/</link>
		<comments>http://icodeblog.com/2010/09/03/adding-local-weather-conditions-to-your-app-part-12-implementing-corelocation/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 22:45:12 +0000</pubDate>
		<dc:creator><![CDATA[Matt Tuzzolo]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Coordinates]]></category>
		<category><![CDATA[CoreLocation]]></category>
		<category><![CDATA[GPS]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[Latitude]]></category>
		<category><![CDATA[Longitude]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2228</guid>
		<description><![CDATA[Knowing the latitude and longitude of your users can open up all kinds of possibilities in your apps.  In an upcoming post, we&#8217;ll be discussing how you can use your user&#8217;s location to determine their local weather conditions and forecast.  But for now, we&#8217;re going to focus on Part 1 of this two part tutorial: CoreLocation.
Apple&#8217;s done a great job of abstracting GPS, Cellular Triangulation, and Wifi Access Point location lookups into CoreLocation; making it extremely easy to  ...]]></description>
				<content:encoded><![CDATA[<p>Knowing the latitude and longitude of your users can open up all kinds of possibilities in your apps.  In an upcoming post, we&#8217;ll be discussing how you can use your user&#8217;s location to determine their local weather conditions and forecast.  But for now, we&#8217;re going to focus on Part 1 of this two part tutorial: CoreLocation.</p>
<p>Apple&#8217;s done a great job of abstracting GPS, Cellular Triangulation, and Wifi Access Point location lookups into CoreLocation; making it extremely easy to determine the approximate location of your user regardless of their device and network connectivity.  Additionally, Apple&#8217;s new iPhone Simulator finally supports CoreLocation as well.  This significantly eases the process of testing your location code.</p>
<p>The first thing you&#8217;ll need to do is add the CoreLocation framework to your project.  This is pretty straightforward.  Command-Click on &#8216;Frameworks&#8217; -&gt; Add -&gt; Existing Frameworks</p>
<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-03-at-12.00.35-PM1.png"><img class="alignnone size-medium wp-image-2232" title="Screen shot 2010-09-03 at 12.00.35 PM" src="/wp-content/uploads/2010/09/Screen-shot-2010-09-03-at-12.00.35-PM-300x193.png" alt="" width="300" height="193" /></a></p>
<p>Select &#8216;CoreLocation.framework&#8217; and click &#8216;add&#8217;.</p>
<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-03-at-12.00.49-PM1.png"><img class="alignnone size-medium wp-image-2231" title="Screen shot 2010-09-03 at 12.00.49 PM" src="/wp-content/uploads/2010/09/Screen-shot-2010-09-03-at-12.00.49-PM-191x300.png" alt="" width="191" height="300" /></a></p>
<p>The class below implements the LocationManager delegate methods required to get the user&#8217;s location.  You can just as easily implement the LocationManagerDelegate protocol in your AppDelegate or elsewhere in your App.  Though I&#8217;ve found that having this class makes it super easy to drop in location support into new projects instead of having to cut/paste delegate methods (ugly).  Anyway.  Take a look:</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;">//  LocationGetter.h</span>
<span style="color: #11740a; font-style: italic;">//  CoreLocationExample</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt on 9/3/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2009 iCodeBlog. 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 &lt;coreLocation/CoreLocation.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@protocol</span> LocationGetterDelegate
@required
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> newPhysicalLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>location;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@interface</span> LocationGetter <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>  <span style="color: #002200;">&#123;</span>
    CLLocationManager <span style="color: #002200;">*</span>locationManager;
    <span style="color: #a61390;">id</span> delegate;
<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>startUpdates;
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> CLLocationManager <span style="color: #002200;">*</span>locationManager;
<span style="color: #a61390;">@property</span><span style="color: #002200;">&#40;</span>nonatomic , retain<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">id</span> delegate;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Notice that we&#8217;re defining our own protocol with a method that takes the new CLLocation as a parameter.  We&#8217;ll be implementing that delegate method in a minute.  Now for the class</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">&nbsp;</pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//  LocationGetter.m</span>
<span style="color: #11740a; font-style: italic;">//  CoreLocationExample</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt on 9/3/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2009 iCodeBlog. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;LocationGetter.h&quot;</span>
<span style="color: #6e371a;">#import &lt;coreLocation/CoreLocation.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> LocationGetter
&nbsp;
<span style="color: #a61390;">@synthesize</span> locationManager, delegate;
<span style="color: #a61390;">BOOL</span> didUpdate <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</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>startUpdates
<span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Starting Location Updates&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>locationManager <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>
        locationManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CLLocationManager alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
    locationManager.delegate <span style="color: #002200;">=</span> self;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// locationManager.distanceFilter = 1000;  // update is triggered after device travels this far (meters)</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Alternatively you can use kCLLocationAccuracyHundredMeters or kCLLocationAccuracyHundredMeters, though higher accuracy takes longer to resolve</span>
    locationManager.desiredAccuracy <span style="color: #002200;">=</span> kCLLocationAccuracyKilometer;
    <span style="color: #002200;">&#91;</span>locationManager startUpdatingLocation<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>locationManager<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocationManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>manager 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>
    UIAlertView <span style="color: #002200;">*</span>alert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Error&quot;</span> message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Your location could not be determined.&quot;</span> delegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;OK&quot;</span> otherButtonTitles<span style="color: #002200;">:</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alert release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Delegate method from the CLLocationManagerDelegate protocol.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>locationManager<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocationManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>manage didUpdateToLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newLocation fromLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>oldLocation
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>didUpdate<span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span>;
&nbsp;
    didUpdate <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
    <span style="color: #11740a; font-style: italic;">// Disable future updates to save power.</span>
    <span style="color: #002200;">&#91;</span>locationManager stopUpdatingLocation<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// let our delegate know we're done</span>
    <span style="color: #002200;">&#91;</span>delegate newPhysicalLocation<span style="color: #002200;">:</span>newLocation<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>dealloc
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>locationManager release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <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>CoreLocation gives you a few options for the accuracy of the user&#8217;s location.  The more accurate the measurement, typically the longer it takes LocationManager to call it&#8217;s delegate method didUpdateToLocation.  It&#8217;s just something to keep in mind when deciding what level of accuracy to use.</p>
<p>Next we need to actually invoke this code and start getting location updates.  I usually do this in my AppDelegate&#8217;s didFinishLaunchingWithOptions, though you could also do it in viewDidLoad somewhere if you didn&#8217;t need to know the user&#8217;s location on app startup.</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;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions <span style="color: #002200;">&#123;</span>
&nbsp;
    UIActivityIndicatorView <span style="color: #002200;">*</span>spinner <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIActivityIndicatorView alloc<span style="color: #002200;">&#93;</span> initWithActivityIndicatorStyle<span style="color: #002200;">:</span>UIActivityIndicatorViewStyleWhiteLarge<span style="color: #002200;">&#93;</span>;
	spinner.center <span style="color: #002200;">=</span> CGPointMake<span style="color: #002200;">&#40;</span>self.viewController.view.frame.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>, self.viewController.view.frame.size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>spinner startAnimating<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>viewController.view addSubview<span style="color: #002200;">:</span>spinner<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// get our physical location</span>
    LocationGetter <span style="color: #002200;">*</span>locationGetter <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>LocationGetter alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    locationGetter.delegate <span style="color: #002200;">=</span> self;
    <span style="color: #002200;">&#91;</span>locationGetter startUpdates<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Add the view controller's view to the window and display.</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;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Notice that I&#8217;ve set locationGetter&#8217;s delegate to self.  So in your .h, make sure to add LocationGetterDelegate to the interface.</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;">//  CoreLocationExampleAppDelegate.h</span>
<span style="color: #11740a; font-style: italic;">//  CoreLocationExample</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Matt Tuzzolo on 9/3/10.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright iCodeBlog 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;LocationGetter.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@class</span> CoreLocationExampleViewController;
&nbsp;
<span style="color: #a61390;">@interface</span> CoreLocationExampleAppDelegate <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>  <span style="color: #002200;">&#123;</span>
    UIWindow <span style="color: #002200;">*</span>window;
    CoreLocationExampleViewController <span style="color: #002200;">*</span>viewController;
    CLLocation <span style="color: #002200;">*</span>lastKnownLocation;
<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 UIWindow <span style="color: #002200;">*</span>window;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet CoreLocationExampleViewController <span style="color: #002200;">*</span>viewController;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> CLLocation <span style="color: #002200;">*</span>lastKnownLocation;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>I&#8217;ve also added a CLLocation *lastKnownLocation that we&#8217;ll use in our delegate method (which comes next):</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;"># pragma mark -</span>
<span style="color: #6e371a;"># pragma mark LocationGetter Delegate Methods</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>newPhysicalLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>location <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Store for later use</span>
    self.lastKnownLocation <span style="color: #002200;">=</span> location;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Remove spinner from view</span>
    <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>UIView <span style="color: #002200;">*</span>v <span style="color: #a61390;">in</span> <span style="color: #002200;">&#91;</span>self.viewController.view subviews<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>v class<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span>UIActivityIndicatorView class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span>v removeFromSuperview<span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">break</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Alert user</span>
    UIAlertView <span style="color: #002200;">*</span>alert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Location Found&quot;</span> message<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;Found physical location.  %f %f&quot;</span>, self.lastKnownLocation.coordinate.latitude, self.lastKnownLocation.coordinate.longitude<span style="color: #002200;">&#93;</span> delegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;OK&quot;</span> otherButtonTitles<span style="color: #002200;">:</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alert release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// ... continue with initialization of your app</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>This last piece takes care of storing the location, removing the spinner, and firing off an alert.  If this was your code, you&#8217;d probably want to re-enable any UI elements that you&#8217;ve disabled and let the user start using your app.</p>
<p>Now build and run your app and you should see:</p>
<p><a href="/wp-content/uploads/2010/09/Screen-shot-2010-09-07-at-9.45.33-AM1.png"><img src="/wp-content/uploads/2010/09/Screen-shot-2010-09-07-at-9.45.33-AM1.png" alt="" title="Screen shot 2010-09-07 at 9.45.33 AM" width="280" height="516" class="alignnone size-full wp-image-2307" /></a></p>
<p>That&#8217;s it!</p>
<p>Here&#8217;s the <a href="/wp-content/uploads/2010/09/CoreLocationExample1.zip">Example Project</a> for the post.</p>
<p>For those of you who don&#8217;t know me yet, my name is Matt Tuzzolo (<a href="http://www.twitter.com/matt_tuzzolo">@matt_tuzzolo</a>).  This is my first iCodeBlog post.  I hope you enjoyed it.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/09/03/adding-local-weather-conditions-to-your-app-part-12-implementing-corelocation/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
	</channel>
</rss>
