Location Detail in iPhone

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.


Similar Articles