i want to get my current location lat long for this i have written some code but i getting wrong place's lat long
here is my code.
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
float latitude = locationManager.location.coordinate.latitude;
float longitude = locationManager.location.coordinate.longitude;
To get the current location, write this code:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
Then implement the delegate methods:
// CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// Handle location updates
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
// Handle error
}
Set the delegate correctly: [locationManager setDelegate:self]
Implement the correct delegate method locationManager:didUpdateLocations:
Read out the locations array. The last item in the array is the most recent found location.
Related
I am working on the Location update in my application
When the app is with internet connection location update (didUpdateLocation) is called. But when the app goes offline, Location update delegate is not called in iOS 14 . I am not getting the latitude and longitude.
Any idea why this is happening or is this the process of location manager?
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
if([locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]){
[locationManager requestWhenInUseAuthorization];
}else{
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
lattitude = [NSString stringWithFormat:#"%f", location.coordinate.latitude] ;
longitude = [NSString stringWithFormat:#"%f",location.coordinate.longitude];
[locationManager stopUpdatingLocation];
}
I have implemented geofence in my app. CLRegion radius is 200m. CLRegion boundary Exitregion delegate method triggers even when the device is in a stable position within CLRegion region.
Here is my code
locationmanager = [[CLLocationManager alloc] init];
locationmanager.delegate = self;
locationmanager.distanceFilter = kCLDistanceFilterNone;
locationmanager.desiredAccuracy = kCLLocationAccuracyBest;
[locationmanager requestAlwaysAuthorization];
if (locationmanager.locationServicesEnabled) {
[locationmanager startUpdatingLocation];
}
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(13.051404,
80.179970);
CLRegion *bridge = [[CLCircularRegion alloc]initWithCenter:center
radius:200.0
identifier:#"Bridge"];
[locationmanager startMonitoringForRegion:bridge];
[locationmanager startMonitoringSignificantLocationChanges];
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(#"location notific %f %f",oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
NSLog(#"location notific2 %f %f",newLocation.coordinate.latitude, newLocation.coordinate.longitude);
[locationManager stopUpdatingLocation];
}
Can anyone suggest me is there any solution for this?
You’re not using the geofence delegate method. You’re using the standard location delegate method. Search the docs for locationManager:didEnterRegion: and didExitRegion methods
Having a bit of an issue. I have a GMSMapView in my application and it points to the users location upon loading. When the app loads I have the locationManager set to display the latitude in the log once it finds the users position. Right now it displays the latitude over 3 times (I plan to make this post to my server). When I launch my application I will have quite a few users and the extra 3 server requests per user would be a lot for a server to handle. I am wondering is there anything I have done wrong in the code or is there a way to get around this? Will attach the code below.
Thanks!
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:(id)self];
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startMonitoringSignificantLocationChanges];
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self.locationManager stopUpdatingLocation];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:newLocation.coordinate.latitude
longitude:newLocation.coordinate.longitude
zoom:17.0];
[self.mapView animateToCameraPosition:camera];
latitude = newLocation.coordinate.latitude;
longitude = newLocation.coordinate.longitude;
NSLog(#"latitude = %g", latitude);
}
didUpdateToLocation was deprecated way back in iOS6. Use the replacement didUpdateLocations, and then just pick off the last element of the array e.g.:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
location = [locations lastObject];
// ...
latitude = location.coordinate.latitude;
longitude = location.coordinate.longitude;
I have been trying to fake my location in the iOS simulator but the methods I have been reading about are not working. I have used the debugger to set my fake location, and have made a custom location in Debug->Location->Custom Location, yet I still log 0.000 0.000 for lat and long.
I'm using this code to find current location.
- (IBAction)currentLocationButtonPressed:(UIButton *)sender
{
//Search for pubs and bars in current location.
//Push to tableViewController
NSLog(#"current location Pressed.");
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest; // Best accuracy
[locationManager startUpdatingLocation];
NSLog(#"%#",[self deviceLocation]);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSLog(#"Latitude: %f Longitude: %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
}
}
- (NSString *)deviceLocation {
return [NSString stringWithFormat:#"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
}
The delegate is set and I am requesting authorization.
- (void)viewDidLoad {
[super viewDidLoad];
searchLocationTextField.delegate = self;
locationManager.delegate = self;
locationManager = [[CLLocationManager alloc]init];
[locationManager requestWhenInUseAuthorization]; // For foreground access
[locationManager requestAlwaysAuthorization]; // For background access
}
Set locationManager delegate to after allocating it.
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager:didUpdateToLocation:fromLocation: delegate method is deprecated, use locationManager:didUpdateLocations: instead.
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = [locations lastObject];
NSLog(#"Latitude: %f Longitude: %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
}
According my app requirement, we do tracking on, and its working fine. but problems comes when i enable GPS service on and never moves and place iPhone on desk for 1 hour after that i do journey of 30 minutes but my update location delegate never called this time.below is my code:
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.distanceFilter = 150.0f;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"6.0")) {
setPausesLocationUpdatesAutomatically:NO];
}
Write this code :
[locationManager startUpdatingLocation];
& write their delegates methods also :
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSMutableString *latString = [NSMutableString stringWithFormat:#"%f",newLocation.coordinate.latitude];
NSMutableString *lngString = [NSMutableString stringWithFormat:#"%f",newLocation.coordinate.longitude];
}