CLLocationManager not updating location - objective-c

Problem is location manager is not updating location.
The (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLoc fromLocation:(CLLocation *)oldLoc functions gets called but it displays the new location as the same old location.
Following is my piece of code:
In my viewDidLoad method i am creating the object of CLLocationManager
-(void) viewDidLoad
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
// created a timer to call locationUpdate method
[NSTimer scheduledTimerWithTimeInterval:20 target: self selector: #selector(locationUpdate) userInfo: nil repeats: YES];
}
-(void)locationUpdate
{
[self.locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLoc
fromLocation:(CLLocation *)oldLoc
{
NSLog(#"in locationmanager did update %f",newLoc.coordinate.latitude);
MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance(newLoc.coordinate, 0.01, 0.02);
[self.mapView setRegion:region animated:YES];
[self.locationManager stopUpdatingLocation];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.02);
MKCoordinateRegion region = MKCoordinateRegionMake(mapView.userLocation.coordinate, span);
[_mapView setRegion:region animated:YES];
[_mapView regionThatFits:region];
}
The value which i am getting in NSLog(#"in locationmanager did update %f",newLoc.coordinate.latitude) is always the same even-though i had moved for more than 2 kilometers starting from current location.
Please help me to how to get exact new location whenever there is a locationupdate. Thanks in advance.

You are stopping the location manager with
[self.locationManager stopUpdatingLocation];
in
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLoc
fromLocation:(CLLocation *)oldLoc
. This delegate method gets called EVERY time the user moves and can give you old (cached) data at the beginning, so by stopping it right away when you get your first location fix you probably get a cached location each time. The fix is simple, just don't stop the location manager there but rather when your viewController disappears or at a similar useful place.

Related

unable to Get GPS Location "BSXPCMessage received error for message: Connection interrupted"

I am trying to get the Location "Latitude & Longitude".
The steps which I did as follow:
In CurrentLocationViewController.m:
#implementation CurrentLocationViewController {
CLLocationManager *locationManager;
}
Then in CurrentLocationViewController.h
#import <CoreLocation/CoreLocation.h>
#interface CurrentLocationViewController : UIViewController <CLLocationManagerDelegate>
Back to In CurrentLocationViewController.m:
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
locationManager = [[CLLocationManager alloc] init];
}
return self;
}
- (IBAction)getLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError %#", error);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation !=nil) {
self.longitudeLabel.text = [NSString stringWithFormat:#"%0.8f", currentLocation.coordinate.longitude];
self.latitudeLabel.text = [NSString stringWithFormat:#"%0.8f", currentLocation.coordinate.latitude];
}
}
I put Break point on each methods, and even though, when I run the application, i got message in the Console: BSXPCMessage received error for message: Connection interrupted.
Any idea?
I got the same error message, still can't fix 'BSXPCMessage' error, but about the location part, I guess you're running iOS 7 version codes on iOS 8?
If so, try this:
Add [locationManager requestAlwaysAuthorization] or [locationManager requestWhenInUseAuthorization] above [locationManager startUpdatingLocation]
open Info.plist, add NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription into 'information property list' with a message
That's it, hope this can help you.
Getting the same error message when working with AVFoundation. Images and videos. It's not specific to what you do.

Can we show user location in offline in MKMapview

Can we get current user location "That blue ball which is animating" when our device is offline.when i tried to get current user location and log it i'm getting 0.00000 for both longitude and latitude here is the code that i used to get current user location.I'm using ipad mini to test it.I have also added CLLocationManagerDelegate in .h file.
- (void)viewDidLoad {
[self getUserLocation];
self.myMapView.showsUserLocation = YES;
}
-(void)getUserLocation{
if ([CLLocationManager locationServicesEnabled]) {
locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[locationManager startUpdatingLocation];
}else{
NSLog(#"User location Disabled");
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentGPSLocation = newLocation;
if (currentGPSLocation != nil) {
currentLocation.latitude = self.myMapView.userLocation.coordinate.latitude;
currentLocation.longitude = self.myMapView.userLocation.coordinate.longitude;
currentLocationWalk.latitude=self.myMapView.userLocation.coordinate.latitude;
currentLocationWalk.longitude=self.myMapView.userLocation.coordinate.longitude;
NSLog(#"didUpdateToLocation: %f,%f", self.myMapView.userLocation.coordinate.latitude,self.myMapView.userLocation.coordinate.longitude);
statusLabel.textColor = [UIColor blackColor];
statusLabel.text = [NSString stringWithFormat:#"%f",self.myMapView.userLocation.coordinate.latitude];
statusLabel1.text = [NSString stringWithFormat:#"%f",self.myMapView.userLocation.coordinate.longitude];
}
}
You are not using ARC (I strongly suggest using it, it avoid most of the memory management errors!).
Therefore it might be that "locationManager" is a instance variable that is not retained (maybe you did not declare it as a retained property).
In this case, it might have been released already when your code returns to the main run loop, where the autorelease pool is drained.
This line
locationManager = [[[CLLocationManager alloc] init] autorelease];
is not a good idea.
define the locationManager as a property,
init the locationManager in viewDidLoad(), but remove the autorelease!
and relase the locationManager only when the viewController is unloaded (there where you release the other properties)

Circle overlay follow user location

How can i make my custom circle overlay follow as a subview the current user location in real time? I have managed to display my circle overlay and get to the center of userlocation but when user moves (gps) the overlay stays still and does not followuser. How do i fix that? Thank you in advance..
- (void)viewDidLoad {
[super viewDidLoad];
[self.mapView setRegion:MKCoordinateRegionMake(self.location.coordinate, MKCoordinateSpanMake(0.02, 0.02))];
[self configureOverlay];
[[self locationManager] startUpdatingLocation];
}
in configureOverlay:
CircleOverlay *overlay = [[CircleOverlay alloc] initWithCoordinate:self.location.coordinate radius:self.radius];
[self.mapView addOverlay:overlay];
GeoQueryAnnotation *annotation = [[GeoQueryAnnotation alloc] initWithCoordinate:self.location.coordinate radius:self.radius];
[self.mapView addAnnotation:annotation];
I also tried using:
CLLocation *location = _locationManager.location;
CLLocationCoordinate2D coordinate = [location coordinate];
and replacing coordinate with self.location.coordinate
any advise would be appreciated.Thank you
Tell your class you are going to implement CLLocationManagerDelegate
#interface YourClass () <CLLocationManagerDelegate>
Then add the following methods:
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//You can change the overlay coordinates here. Even with an animation if you want.
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSString *errorType = (error.code == kCLErrorDenied) ? #"Access Denied" : #"Unknown Error";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error getting Location"message:errorType delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alert show];
}
The locationManager:didUpdateToLocation... method will be triggered each time the location manager detects a change in the user's position.
Cheers.
EDIT
I forgot about something very important. In your viewDidLoad method add your class as the delegate:
self.locationmanager.delegate = self;

didUpdateToLocation called multiple

i have an iOS application witch uses the current location of the user. I am doing like this :
-(void)startGeoloc{
NSLog(#"start geoloc");
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy=kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate methods
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[locationManager stopUpdatingLocation];
AppDelegate *apDelegate =(AppDelegate *)[UIApplication sharedApplication].delegate;
apDelegate.modeGeoloc = YES;
[self callWebService:locationManager.location];
}
The problem of this, is that my method callWebService:locationManager.location is called twice and i would like to call it just one time. how i can i do this ? thanks for your answers
To ensure locationManager only calls the "didUpdate..." method once, use a BOOL to reference if the location has been found yet.
Create the ivar BOOL:
#property BOOL didFindLocation;
Before locationManager startUpdatingLocation, set the new BOOL to NO. That way you can call for a new location update at will.
-(void) startFindingLocation {
self.didFindLocation = NO; // like this
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self]
[locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
[locationManager startUpdatingLocation];
}
In "didUpdateLocations", check for it.
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
if (!self.didFindLocation) {
self.didFindLocation = YES;
[locationManager stopUpdatingLocation];
// do the rest of your stuff
}
}
If possible, do not set didFindLocation anywhere else in your code to avoid confusion.
locationManager delegate methods can be called very frequently (they didUpdateToLocation all the time, right? :)
One way would be to have your callWebService have state, know whether it is currently executing a request and ignore concurrent requests if one is still going. Another way would be to keep a timestamp and only allow it through if 2 minutes has passed since the previous one.
Had the same problem.
I think the easiest solution is setting the CLLocationManager to null.
locationManager = nil;
after calling
[locationManager stopUpdatingLocation];
locationManager.startUpdatingLocation() fetch location continuously and didUpdateLocations method calls several times,
Just set the value for locationManager.distanceFilter value before calling locationManager.startUpdatingLocation().
As I set 100 meters(you can change as your requirement) working fine, and will work for you.
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 100
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()

problem in getting the altitude value using CLLocationManager in iphone sdk

I am using the code as I specified below to get the altitude value
-(void)awakeFromNib {
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
[locmanager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
heightMesurement.text = [NSString stringWithFormat: #"%.2f m", newLocation.altitude];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
heightMesurement.text = #"0.00 m";
}
The didUpdateToLocation method is calling and I am getting the altitude value as null when I print heightMesurement.text.
Can anyone give me idea why this happening like this and how to rectify it.
Thanks to all,
Monish.