ARTICLE

Location Detail in iPhone

Posted by Sachin Bhardwaj Articles | iPhone/iPad December 13, 2012
In this article I will explain how to manage location details for an iPhone.
Reader Level:
Download Files:
 

Introduction

In this article I will create a Single View application. Here I use a label, button from outlet and to show location detail in the iPhone device we add a framework core location.

To better understand it we use the following procedure.

Step 1

Open XCode by double-clicking on it.

Step 2

Create a New XCode Project by clicking on it.

Step 3

Now Select Single View Application and click on Next.

Step 4

Now provide your Product Name and Company Identifier. 

Step 5

Select the location where you want to save your project and click on Create.

Step 6 

Now initially we add the framework CoreLocation which is required to show the location of the device.

To import this framework we use the following.

Step 7

Click on project and select Build Phase.

build-phase-in-iPhone.jpg

Step 8

Click on the "+" icon to add a framework.

Step 9

Enter into the search bar Corelocation and click on the add button.

core-location-in-iPhone.jpg

Step 10

Now we write the code of the Objective-C classes:

CoreLocationViewController.h

//
// CoreLocationViewController.h
// LocationDetail
//
// Created by Sachin Bhardwaj on 13/12/12.
// Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface CoreLocationViewController : UIViewController
<CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) IBOutlet UILabel *latitude;
@property (strong, nonatomic) IBOutlet UILabel *longitude;
@property (strong, nonatomic) IBOutlet UILabel *horizontalAccuracy;
@property (strong, nonatomic) IBOutlet UILabel *verticalAccuracy;
@property (strong, nonatomic) IBOutlet UILabel *altitude;
@property (strong, nonatomic) IBOutlet UILabel *distance;
@property (strong, nonatomic) CLLocation *startLocation;
- (IBAction)resetDistance;
@end

CoreLocationViewController.m

//
// CoreLocationViewController.m
// LocationDetail
//
// Created by Sachin Bhardwaj on 13/12/12.
// Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import "CoreLocationViewController.h"

@interface CoreLocationViewController ()

@end

@implementation CoreLocationViewController
@synthesize longitude, latitude, horizontalAccuracy, verticalAccuracy, altitude, distance;
@synthesize locationManager, startLocation;

-(void)resetDistance
{
startLocation = nil;
}

- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
startLocation = nil;}

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSString *currentLatitude = [[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.coordinate.latitude];
latitude.text = currentLatitude;

NSString *currentLongitude = [[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.coordinate.longitude];
longitude.text = currentLongitude;

NSString *currentHorizontalAccuracy =
[[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.horizontalAccuracy];
horizontalAccuracy.text = currentHorizontalAccuracy;

NSString *currentAltitude = [[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.altitude];
altitude.text = currentAltitude;

NSString *currentVerticalAccuracy =
[[NSString alloc]
initWithFormat:@"%+.6f",
newLocation.verticalAccuracy];
verticalAccuracy.text = currentVerticalAccuracy;

if (startLocation == nil)
self.startLocation = newLocation;

CLLocationDistance distanceBetween = [newLocation
distanceFromLocation:startLocation];

NSString *tripString = [[NSString alloc]
initWithFormat:@"%f",
distanceBetween];
distance.text = tripString;
}
-(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Location error");
}

@end

CoreLocationViewController.Xib
 file-owner-in-iPhone.jpg


Step 11

Click on the run button to show output.

Step 12

Output 1 in iPhone:

output1-in-iphone.jpg

When the application executes it will show this alert massage.

Output 2 in iPhone:

output2-in-iphone.jpg

When we click on the Ok button then the output is shown in the simulator.

Output 3 in iPhone:

output3-in-iphone.jpg

When we click on Don't Allow then the output is shown in the simulator.

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts