How To Get Current Latitude And Longitude In iPhone

  • Home >>
  • iOS
  • >> How To Get Current Latitude And Longitude In iPhone

In this application we will see how to get Current latitude and longitude using GPRS in the iPhone. So let see how it will worked. My last post you can get from here (attached the previous post link).

Step 1: Open the Xcode, Create a new project using View Base application. Give the application “Current_LatLong”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Expand classes and notice Interface Builder created the ViewController class for you. Expand Resources and notice the template generated a separate nib,.xib for the Current_LatLong application.

Step 4: We need to add CoreLocation.framework in the project.

Step 5: Open the Current_LatLongViewController.h file and make the following changes:

#import <uikit/UIKit.h>
#import <coreLocation/CoreLocation.h>
@interface Current_LatLongViewController : UIViewController {
CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocationManager *locationManager;

Step 6: Double click the Current_LatLongViewController.xib file and open it to the Interface Builder. First drag the Round Rect Button from the library and place it to the view window. Now select the Round Rect button and bring up Connection Inspector and connect Touch Up inside to the File’s Owner icon and select Button: method. Now save the .xib file close it and go back to the Xcode.

Step 7: Open the Current_LatLongViewController.m file and make the following changes:

#import "Current_LatLongViewController.h"
@implementation Current_LatLongViewController
@synthesize locationManager;
- (void)dealloc
[super dealloc];
- (void)didReceiveMemoryWarning
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
#pragma mark - View lifecycle
- (void)viewDidUnload
[super viewDidUnload];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
NSLog(@"dLatitude : %@", latitude);
NSLog(@"dLongitude : %@",longitude);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 40, 250, 50)];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 80, 200, 50)];
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(50, 120, 200, 100)];
myLabel.textColor = [UIColor blackColor];
myLabel1.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
myLabel.backgroundColor = [UIColor clearColor];
myLabel1.backgroundColor = [UIColor clearColor];
[myLabel setText:latitude];
[myLabel1 setText:longitude];
label.text = @"Current Latitude And Longitude";
[self.view addSubview:label];
[self.view addSubview:myLabel];
[self.view addSubview:myLabel1];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);

Step 8: Now compile and run the application on the actual device.

  • Rick
  • January 10, 2012
  • iOS

About the Author

Leave a Reply 3 comments

Sagi - January 19, 2012 Reply

The Controller is used as a delegate, but the h file does not declare the required delegate in code.
Should be :
@interface Current_LatLongViewController : UIViewController

Vasilis - May 22, 2012 Reply

Everything works perfect , but i can only see the map. How can i see my position with a pin on the map?

Leave a Reply: