How to get the user’s current location by code in iphone app?

We have to use CLLocationManager in our app to track the current  location. Try the below sample code

In .h file                                                                                                               
 
#include <CoreLocation/CLLocationManagerDelegate.h>
#include <CoreLocation/CLError.h>
#include <CoreLocation/CLLocation.h>
#include <CoreLocation/CLLocationManager.h>
CLLocationManager   * myLocationManager;
CLLocation          * myLocation;
 
In .m file
 
-(void)findMyCurrentLocation
   
{          

   
self.myLocationManager = [[[CLLocationManager alloc] init] autorelease];
   
[[self myLocationManager] setDelegate:self ];
   
[myLocationManager startUpdatingLocation];

   
double latitude=34.052234;
   
double longitude=-118.243685;

   
CLLocation *defaultLocation =[[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
   
[self setMyLocation:defaultLocation];
   
[defaultLocation release];

   
if( [CLLocationManager locationServicesEnabled] )
   
{  
       
NSLog(@"Location Services Enabled....");
        locationServicesEnabled
=TRUE;
       
UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:@"Information"
                                                         message
:@"Fetching your current location."
                                                       
delegate:self
                                               cancelButtonTitle
:@"OK"
                                               otherButtonTitles
:nil ];
       
[alert release];
   
}
   
else
   
{  
       
NSLog( @"Location Services Are Not Enabled...." );
        locationServicesEnabled
=FALSE;
       
UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:@"Information"
                                                         message
:@"Location service is not enable. Please enable it from settings."
                                                       
delegate:self
                                               cancelButtonTitle
:@"OK"
                                               otherButtonTitles
:nil ];
       
[alert release];
     
}

       
}

   
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
   
{
     
[self setMyLocation:newLocation];

   
NSString *tempLat = [ NSString stringWithFormat:@"%3.6f" , (newLocation.coordinate.latitude) ];
   
NSString *tempLong= [ NSString stringWithFormat:@"%3.6f" , (newLocation.coordinate.longitude)];

    appDelegate
.curlat = tempLat;
    appDelegate
.curlong = tempLong;
   
}

     
- (void)locationManager: (CLLocationManager *)manager didFailWithError: (NSError *)error
     
{
    printf
("nerror");
   
UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:@"Error"
                                                     message
:@"Error while getting your current location."
                                                   
delegate:self
                                           cancelButtonTitle
:@"OK"
                                           otherButtonTitles
:nil ];

   
[alert release];
     
}
All the best 
                   

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>