IT tutorials
 
Mobile
 

Iphone Application : Implementing Location Services - Creating a Location-Aware Application (part 1)

12/12/2012 6:31:21 PM
- Windows 10 Product Activation Keys Free 2019 (All Versions)
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire

Many iPhone and Mac users have a, shall we say, “heightened” interest in Apple Computer; visiting Apple’s campus in Cupertino, California, can be a life-changing experience. For these special users, we’re going to create a Core Location-powered application that keeps you informed of just how far away you are.

The application will be created in two parts: The first introduces Core Location and displays the number of miles from the current location to Cupertino. In the second section, we use the iPhone’s compass to display an arrow that points users in the right direction, should they get off track.

Setting Up the Project

For the rest of this hour, we’ll be working on a new application that uses the Core Location framework. Create a new view-based iPhone application in Xcode and call it Cupertino.

Adding the Core Location Framework

The Core Location framework isn’t linked into our project by default, so we need to add it. Open the Targets group in Xcode, and right-click the Cupertino target. Right-click the Frameworks group in the Cupertino project, and then choose Add, Existing Frameworks from the contextual menu. Scroll through the Framework list that appears and find CoreLocation.framework. Select it, and then click Add to add it to the project, as shown in Figure 1. If it doesn’t add directly to the Frameworks group, drag the CoreLocation.framework icon into the group to keep the project tidy.

Figure 1. Add the Core Location framework to the project.

Adding Background Image Resources

To make sure the user remembers where we’re going, we have a nice picture of an apple as the application’s background image. Locate apple.png within the project’s Images folder in the Finder, and drag it into the Resources group within the Xcode project group. Be sure to check the Copy Items into Destination Group’s Folder check box in the Copy dialog. (You can also include the apple@2x.png file if you want a higher-resolution version for the iPhone 4’s display.)

Adding Outlets, Properties, and Protocols

The CupertinoViewController will serve as the location manager delegate, receiving location updates and updating the user interface to reflect the new locations. Click the CupertinoViewController.h file in the Classes group. Update the file by importing the Core Location header file, indicating that we’ll be conforming to the CLLocationManagerDelegate protocol, and adding properties for the location manager (locMan), a label with the distance to Cupertino (distanceLabel), and two subviews (distanceView and waitView), as demonstrated in Listing 1.

Listing 1.
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface CupertinoViewController : UIViewController
    <CLLocationManagerDelegate> {

        CLLocationManager *locMan;
        IBOutlet UILabel *distanceLabel;
        IBOutlet UIView *distanceView;
        IBOutlet UIView *waitView;

}

@property (assign, nonatomic) CLLocationManager *locMan;
@property (retain, nonatomic) UILabel *distanceLabel;
@property (retain, nonatomic) UIView *distanceView;
@property (retain, nonatomic) UIView *waitView;

@end

For each of the properties, add a corresponding @synthesize line to the CupertinoViewController.m implementation file, following the @implementation directive:

@synthesize locMan;
@synthesize distanceLabel;
@synthesize distanceView;
@synthesize waitView;

Finally, add a release for each of these objects to the implementation file’s dealloc method:

- (void)dealloc {
    [locMan release];
    [distanceLabel release];
    [distanceView release];
    [waitView release];
    [super dealloc];
}

Creating the User Interface

The user interface for this hour’s lesson is simple: We can’t perform any actions to change our location (teleportation isn’t yet possible), so all we need to do is update the screen to show information about where we are.

Double-click the CupertinoViewController.xib file in the Resources group to open Interface Builder, and then complete the following steps:

1.
Start by adding a UIImageView onto the view and center it so that it covers the entire view. This will serve as the background image for the application.

2.
With the image view selected, open the Attributes Inspector (Command+1). Select apple.png from the Image drop-down list.

3.
Next, drag a UIView on top of the image view. Size it to about 80 points high and up to the left, right, and bottom edge guidelines. This view will serve as our primary information readout.

4.
Select the UIView and open the Attributes Inspector (Command+1). Click the Background color and set the background to black. Change the Alpha to 0.75 and check the Hidden check box.

5.
Add a UILabel to the UIView. Size the label up to all four edge guidelines and change the text to read Lots of miles to the Mothership.

6.
Select the label and open the Attributes Inspector (Command+1). Click the center Layout button, uncheck the Adjust to Fit check box for the Font Size attribute, and change the text color to white. The view should now look like Figure 2



Figure 2. The beginnings of the Cupertino Locator UI.


7.
Create a second semitransparent UIView with the same attributes as the first, but not hidden, and with a height of 77 points. Drag the second view to vertically center it on the background.

8.
Add a new label to the second view that reads Checking the Distance. Resize the label so that it takes up approximately the right two-thirds of the semitransparent view.

9.
From the library, drag a UIActivityIndicatorView to the new semitransparent view. Follow the guides to neatly align the activity indicator to the left of the label.

10.
With the activity indicator selected, open the Attributes Inspector (Command+1). Check the check box on the Animated attribute.

11.
Open the Document window. Control-drag from the File’s Owner icon to the Lots of Miles label, choosing the distanceLabel output when prompted. Do the same for the two views, connecting the view with the activity indicator to the waitView outlet and the view that contains the distance estimate to the distanceView outlet.

The final view should resemble Figure 3. When satisfied with your results, save the XIB file and return to Xcode.

Figure 3. The final Cupertino Locator UI.
 
Others
 
- Iphone Application : Implementing Location Services - Understanding Core Location
- Windows Phone 8 : Creating a New Project (part 2) - XAML
- Windows Phone 8 : Creating a New Project (part 1) - Visual Studio
- Developing BlackBerry Tablet Applications : Permissions and Configuration Settings
- Developing BlackBerry Tablet Applications : Permissions
- Introducing Windows Phone 8 : The Marketplace
- Introducing Windows Phone 8 : Application Lifecycle, Driving Your Development with Services, Live Tiles
- Introducing Windows Phone 8 : Input Patterns
- Iphone Application : Using Address Book, Email, and Maps (part 4) - Using the Message UI
- Iphone Application : Using Address Book, Email, and Maps (part 3) - Using a Map Object
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
programming4us programming4us
 
Popular tags
 
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS