How can I get GPS location in Iphone - iphone

Can you tell me, how can I get GPS location in iPhone.
Thanks.

Implement CLLocationManagerDelegate and write this function and call this in viewDidLoad Method of your viewController.
-(void)startLocationManager
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D coordinate = [newLocation coordinate];
double dblLatitude = coordinate.latitude;
double dblLongitude = coordinate.longitude;
}

If you are using CLLocationmanager, make sure you check to see if location Service is enabled first. I would do this to get the current location:
if ([CLLocationManager locationServicesEnabled])
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
CLLocationCoordinate2D *currentLocation = locationManager.location;
[locationManager stopUpdatingLocation];
[locationManager release];
}
If you are using mapView, you could get current location using:
[mapView setShowsUserLocation:YES];
CLLocationCoordinate2D *currentLocation = mapView.userLocation.coordinate;

You use CoreLocation framework.
Here you can get sample code http://mobileorchard.com/hello-there-a-corelocation-tutorial/

Related

How to call a service in the - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation only once?:

I had an application in which i need to call a service whenever the specified location change occurs.I had initiating the location controller in my delegate class, like this
locationController = [[UILocation alloc] init];
locationController.delegate = self;
locationController.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationController.locationManager.distanceFilter = 1000.0;
[locationController.locationManager startUpdatingLocation];
But as everybody knows Accuracybest will call the didupdate methode multiple times in an inconsistent manner. so how can i call my service from the delegate methode only once?Can anybody point me how to achieve this?
-(void)postCurrentLocationOfDevice
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
self.locationManager.delegate = self;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CURRENT_LOCATION = [locations objectAtIndex:0];
[self.locationManager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CURRENT_LOCATION = newLocation;
[self.locationManager stopUpdatingLocation];
}
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[manager stopUpdatingLocation];
}
It will stop location update when first location update occur.

CLLocation Manager not updating new location

I am calling CLLocationManager and it's calling its delegate method as well. But my problem is, isn't updating its New Location after traveling 1 km.
Here's my code:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateLocation1:) userInfo:nil repeats:YES];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
// Method did update location - Update location when location change
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// this method is calling after every 1 sec interval time..
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// this method is not calling once also after travelling around a km ....
}
What am I doing wrong?
You should be calling startUpdatingLocation or startMonitoringSignificantLocationChanges on the location manager for it to start checking for location changes and calling your delegate methods.
locationManager:didUpdateToLocation:fromLocation: is deprecated so you can expect it not to always be used.
If locationManager:didUpdateLocations: is being called then you are receiving location updates.
- (void)viewDidLoad
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//[manager stopUpdatingLocation];
CLLocationCoordinate2D coordinate_currentlocation = [newLocation coordinate];
float latitude_current = newLocation.coordinate.latitude;
float longitude_current = newLocation.coordinate.longitude;
}
Your first method
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// this method is calling after every 1 sec interval time..
}
is used since iOS 6.
The second one is deprecated since iOS 6 and was used prior to iOS 6. You can use both methods, depending on the system version on the device your app is running at, by adding a helper method.
- (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation *) oldLocation
{
[self locationManager:manager helperForLocation:newLocation];
}
- (void) locationManager:(CLLocationManager *) manager didUpdateLocations:(NSArray *) locations
{
if([locations count] > 0)
{
[self locationManager:manager helperForLocation:[locations objectAtIndex:[locations count] - 1]];
}
}
- (void) locationManager:(CLLocationManager *) manager helperForLocation:(CLLocation *) newLocation
{
// your code what to do with location goes here
}
The locations retrieved by iOS 6 are wrapped in a list and could be more than 1. In my example I take the last one and put it to my helper.
i have done this within my app with the use of this
myLocationManager = [[CLLocationManager alloc] init];
myLocationManager.delegate = self;
myLocationManager.desiredAccuracy = kCLLocationAccuracyBest;
[myLocationManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error" message:#"Failed to Get Your Location, Please turn on GPS and Restart The Application"delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
[NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
[NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
// Stop Location Manager
[myLocationManager stopUpdatingLocation];
}
-(void)postCurrentLocationOfDevice
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
self.locationManager.delegate = self;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CURRENT_LOCATION = [locations objectAtIndex:0];
[self.locationManager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CURRENT_LOCATION = newLocation;
[self.locationManager stopUpdatingLocation];
}

not able find location on iPhone device

I have added code for finding location, this is working on simulator but it is not working on iPhone device. I have enabled the location services but still it is not working.
-(void)firstTimeRefresh {
CLLocation *bestLocation = [[[AppDelegate getAppDelegate] locationManager] bestLocation];
if ( bestLocation != NULL) {
NSLog(#"Using Best Loaction");
[self getArray: [NSArray arrayWithObjects: [NSNumber numberWithFloat:bestLocation.coordinate.latitude],
[NSNumber numberWithFloat: bestLocation.coordinate.longitude], nil] :#"false":page_no];
}
}
try this code and its working at mine end.
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
Define the CLLocationManagerDelegate delegate in .h class file
when location is updated this function will get called.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
}

Get current location using CLLocationCoordinate2D

I am trying to get current location of user using CLLocationCoordinate2D. I want the values in the format like
CLLocationCoordinate2D start = {-28.078694,153.382844 };
So that I can use them like following:
NSString *urlAddress = [NSString
stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
start.latitude, start.longitude,
destination.latitude, destination.longitude];
I used
CLLocation *location = [[CLLocation alloc]init ];
CLLocationDegrees currentLatitude = location.coordinate.latitude;
CLLocationDegrees currentLongitude = location.coordinate.longitude;
to get current lat & long.
But I am getting 0.000 for both when I try to test. I am testing on iPhone 4s.
If there is any sample code, it will be great.
Add CoreLocation framework first and then use the following code...and your viewController must implement CLLocationManagerDelegate
-(void)findCurrentLocation
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
if ([locationManager locationServicesEnabled])
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *str=[[NSString alloc] initWithFormat:#" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
NSLog(#"%#",str);
}
in AppDelegate.h declare following variable. and implement
CLLocationManagerDelegate delegate.
CLLocationManager *locationManager;
CLLocation *currentLocation;
in AppDelegate.h.m file write following code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
self.currentLocation = newLocation;
self.currentLat = newLocation.coordinate.latitude;
self.currentLong = newLocation.coordinate.longitude;
}
You need to use CLLocationManager to get a user's location. CLLocation is only a class of objects used to represent locations, it can't get user location by it self.
For more details and example, please follow to Apple docs on Location Awareness Programming.
CLLocationCoordinate2D location = [[[mapview userLocation] location] coordinate];
NSLog(#"Location found from Map: %f %f",location.latitude,location.longitude);
and also refer the link(without mapview0
https://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html
use location manager to get lat and long, follow this tutorial for sample code http://www.edumobile.org/iphone/miscellaneous/how-to-get-current-latitude-and-longitude-in-iphone/
-(void)findCurrentLocation
{
if ([CLLocationManager locationServicesEnabled])
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
[self setLocation:[[manager location] coordinate]];
}

How do I get the current location from the location manager?

i want my app to get the exact location (longitude and latitude) of where he is as every second, it is possible ? i have this code so far, every 3 second the app get the current location but it doesnt update if i move...
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog(#"lol");
[locationManager stopUpdatingLocation];
if (wtf == 2) {
[[NSUserDefaults standardUserDefaults] setObject:newLocation forKey:#"old"];
NSLog(#"wtf");
wtf =0;
}
oldLocation = [[NSUserDefaults standardUserDefaults] objectForKey:#"old"];
double rep = [oldLocation distanceFromLocation:newLocation];
NSString *label1 = [NSString stringWithFormat:#"%2.90f",oldLocation.coordinate.latitude];
NSString *label2 = [NSString stringWithFormat:#"%2.90f",newLocation.coordinate.latitude];
if ([label1 isEqual:label2]) {
NSLog(#"penis");
}
labelm.text = label1;
labelkm.text = label2;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
}
-(void)actualise{
[locationManager startUpdatingLocation];
}
- (void)viewDidLoad {
[super viewDidLoad];
wtf = 2;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:#selector(actualise) userInfo:nil repeats:YES];
}
/*
Use this code to initialize and start the location manager:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];
And implement didUpdateToLocation like this:
- (void) locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*) oldLocation
{
// This will be called every time the device has any new location information.
}
There is no need to use any timers.