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”. [...]

Be Sociable, Share!
WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic.

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;
-(IBAction)Button:(id)sender;
@end

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];
}
-(IBAction)Button:(id)sender
{
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);
}
@end

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

Be Sociable, Share!
WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.
 

1 Response » to “How to get Current Latitude and Longitude in iPhone”

  1. Vasilis says:

    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

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG