Sunday, 14 April 2013

How to get longitude and latitude for an iPhone App code ?

It's really easy doing this using CoreLocation.framework. Fast learning tutorial is as below :


Open your AppDelegate.m, and import the "CoreLocationViewController.h".
// Add this line on top of your AppDelegate.m

#import "CoreLocationViewController.h"
Change the application:didFinishLaunchingWithOptions: method in your 'AppDelegate.m' with the code below. First of all create a 'CoreLocationViewController'. Then create a UINavigationController with the 'CoreLocationViewController' as the RootViewController.
Now we need to add the UINavigationController as a subview of the UIWindow.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
 
    CoreLocationViewController *myCoreLocationViewController = [[CoreLocationViewController alloc] 
                                                      initWithNibName:@"CoreLocationViewController" 
                                                      bundle:[NSBundle mainBundle]];
 
    UINavigationController *nav = [[UINavigationController alloc] 
                                   initWithRootViewController:myCoreLocationViewController];
 
    [[self window] addSubview:[nav view]];
 
    [self.window makeKeyAndVisible];
    return YES;
}

Open your 'CoreLocationViewController.xib' and add two objects of type UILabel. Switch to the 'Assistant Editor'. Select the UIView and insert outlets for the two newly added objects. Give the labels the following names. One label 'locationLabelOne' and the other lable 'locationLabelTwo'. You could create outlets by 'ctrl-clicking' a object and drag it to the left side of the 'Assistant Editor'. This two labels are later used to fill with the longitude and latitude of your CoreLocation service.
Now it's time to create your CoreLaction service. We need a CLLocationManager to get the current locationof the users device.
Open your 'CoreLocationViewController.h' and fill it as the code below. First import the CoreLocation service, after that make a CLLocationManager object.

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
 
@interface CoreLocationViewController : UIViewController <CLLocationManagerDelegate> {
    CLLocationManager *locationManager;
}
@property (retain, nonatomic) CLLocationManager *locationManager;
@property (retain, nonatomic) IBOutlet UILabel *locationLabelOne;
@property (retain, nonatomic) IBOutlet UILabel *locationLabelTwo;
 
@end

After that it's time to use the CoreLocation service in your 'CoreLocationViewController.m'. Open your file and change or add the methods below. First of all synthesize your before created CoreLocation service.
#import "CoreLocationViewController.h"
 
@implementation CoreLocationViewController
@synthesize locationManager; 
@synthesize locationLabelOne;
@synthesize locationLabelTwo;
 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
The second change is in the viewDidLoad: method. Initialize your CoreLocation service by changingthat method with the code below. 
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
 
    [self setLocationManager:[[CLLocationManager alloc] init]];
    [locationManager setDelegate:self];
    [locationManager setDistanceFilter:kCLDistanceFilterNone]; 
    [locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    [locationManager startUpdatingLocation];
}

Now it's time to add the new locationManager:didUpdateToLocation:fromLocation: methodFill the method with the code below. This method is called by the CoreLocation service when the device is going from point a to point b. In this method you've the longitude and latitude, and you could do something with that.
In this situation we're adding the current location to the two above added UILabels. You could read the longitudeand latitude from your screen.

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
 
 
    [locationLabelOne setText:[NSString stringWithFormat:@"%f", newLocation.coordinate.latitude]];
 
    [locationLabelTwo setText:[NSString stringWithFormat:@"%f", newLocation.coordinate.longitude]];
}

Finished! Now you're ready to run your project and use the CoreLocation service to get the longitude andlatitude of the users device.

A reference link and code is on this site : 

Find a detailed tutorial and code on this link !

No comments:

Post a Comment