IT tutorials
 
Mobile
 

Iphone Application : Implementing Location Services - Creating a Location-Aware Application (part 2) - Implementing the Location Manager Delegate

12/12/2012 6:32:13 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Implementing the Location Manager Delegate

Based on the XIB we just laid out, the application starts up with a message and a spinner that let the user know we are waiting on the initial location reading from Core Location. We’ll request this reading as soon as the view loads in the view controller’s viewDidLoad method. When the location manager delegate gets a reading, we’ll calculate the distance to Cupertino, update the label, hide the activity indicator view, and unhide the distance view.

Click the CupertinoViewController.m file in the Classes group. Synthesize the four properties we added to the CupertinoViewController header file and release them in the dealloc method.

Uncomment the viewDidLoad method, and instantiate a location manager with the view controller itself as the delegate and a desiredAccuracy of kCLLocationAccuracyThreeKilometers and a distanceFilter of 1,609 meters (1 mile). Start the updates with the startUpdatingLocation method. The implementation should resemble Listing 2.

Listing 2.
- (void)viewDidLoad {
    [super viewDidLoad];

    locMan = [[CLLocationManager alloc] init];
    locMan.delegate = self;
    locMan.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
    locMan.distanceFilter = 1609; // a mile
    [locMan startUpdatingLocation];
}

Now we need to implement the two methods of the location manager delegate protocol. We’ll start with the error condition: locationManager:didFailWithError. In the case of an error getting the current location, we already have a default message in place in the distanceLabel, so we’ll just remove the waitView with the activity monitor and show the distanceView. If the user denied access to Core Location updates, we will also clean up the location manager request. Implement locationManager:didFailWithError as shown in Listing 3.

Listing 3.
- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {

    if (error.code == kCLErrorDenied) {
        // Turn off the location manager updates
        [manager stopUpdatingLocation];
        [locMan release];
        locMan = nil;
    }
    waitView.hidden = YES;
    distanceView.hidden = NO;
}

Our final method (locationManager:didUpdateLocation:fromLocation) will do the dirty work of calculating the distance to Cupertino. To do this, we obviously need a location in Cupertino that we can compare to the user’s current location. According to http://gpsvisualizer.com/geocode, the center of Cupertino, California, is at 37.3229978 latitude, –122.0321823 longitude. Add two constants for these values (kCupertinoLatitude and kCupertinoLongitude) after the #import line in the CupertinoViewController implementation file:

#define kCupertinoLatitude 37.3229978
#define kCupertinoLongitude -122.0321823

This brings us to one more hidden gem in CLLocation. We don’t need to write our own longitude/latitude distance calculations because we can compare two CLLocation instances with the distanceFromLocation method. In our implementation of locationManager:didUpdateLocation:fromLocation, we will create a CLLocation instance for Cupertino and compare it to the instance we get from Core Location to get the distance in meters. We’ll then convert the distance to miles, and if it’s more than 3 miles we show the distance with an NSNumberFormatter used to add a comma if more than 1,000 miles, and if the distance is less than 3 miles, we stop updating the location and congratulate the user on his or her reaching “the Mothership.” Listing 4 provides the complete implementation of locationManager:didUpdateLocation:fromLocation.

Listing 4.
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {

    if (newLocation.horizontalAccuracy >= 0) {

        CLLocation *Cupertino = [[[CLLocation alloc]
                                  initWithLatitude:kCupertinoLatitude
                                  longitude:kCupertinoLongitude] autorelease];
        CLLocationDistance delta = [Cupertino distanceFromLocation: newLocation];
        long miles = (delta * 0.000621371) + 0.5; // meters to rounded miles
        if (miles < 3) {
            // Stop updating the location
            [manager stopUpdatingLocation];
            // Congratulate the user
            distanceLabel.text = @"Enjoy the\nMothership!";
        } else {
            NSNumberFormatter *commaDelimited = [[[NSNumberFormatter alloc]
                                                   init] autorelease];
            [commaDelimited setNumberStyle:NSNumberFormatterDecimalStyle];
            distanceLabel.text = [NSString stringWithFormat:
                                  @"%@ miles to the\nMothership",
                                  [commaDelimited stringFromNumber:
                                  [NSNumber numberWithLong:miles]]];
        }
        waitView.hidden = YES;
        distanceView.hidden = NO;
    }
}

					  

Choose Build and Run and take a look at the result. Your application should, after determining your location, display the distance to Cupertino, California, as shown in Figure 4.

Figure 4. The Cupertino application in action showing the distance to Cupertino, California.


Did you Know?

Location services work in the iOS 4 iPhone Simulator! The simulator uses Snow Leopard’s location services to determine where your computer is sitting and then passes that information to your applications.

 
Others
 
- Iphone Application : Implementing Location Services - Creating a Location-Aware Application (part 1)
- 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
 
 
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
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us