How to get location of mobile in ios programming?

Leave a Comment
Get Mobile Current Location using ios programming in iphone. First of all you need corelocation framwork in iphone. Here i am giving you complete code example for getting Location.

View Controller Header file
ViewController.h


#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>
@end
View Controller implementation file:
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
@end
@implementation ViewController
{
    CLLocationManager *locationManager;
}
- (IBAction)Location:(id)sender {
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
   
    [locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;
   
     
        UIAlertView *AlertLocation = [[UIAlertView alloc]
                                   initWithTitle:[NSString stringWithFormat:@"%.8f", distanceMeters] message:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [AlertLocation show];
//        longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
//        latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    locationManager = [[CLLocationManager alloc] init];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
This example code will give you latest updated current Location on Location button click Event..

0 comments:

Post a Comment