MKMapViewDelegate mapView:didUpdateUserLocation: method is not called when running on the 5.0 simulator, even if all location permissions are given in the device settings.
With 4.3 it's working fine.
Any ideas?
I got the same thing after refactor to ARC.
First I had the following code in viewDidLoad;
CLLocationManager *lm =[[CLLocationManager alloc] init];
lm.delegate = self;
lm.desiredAccuracy = kCLLocationAccuracyBest;
[lm startUpdatingLocation];
I did the following in de headerfile
#interface ViewController : UIViewController <CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
#property(nonatomic, strong)CLLocationManager *locationManager;
And
#synthesize locationManager;
The code I change in viewDidLoad
locationManager =[[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
Check "setUserTrackingMode:animated:" and "userTrackingMode". Both are new in the iOS5. I had a similar problem and fixed it by setting MKUserTrackingModeFollow in the setUserTrackingMode function. I think the default tracking mode is MKUserTrackingModeNone and only calls mapView:didUpdateUserLocation once.
If your problem is that the mapView:didUpdateUserLocation is never called, then take a look at the options in the new xcode, right below the console outputs there's an icon like the gps icon in the ipad, which lets you simulate a location.
I know it's an old thread. I'll answer anyway, it might help others too.
I had similar problem with iOS 6. Which I was able to solve by setting the mapview delegate to self.
Like this:
[mapView setDelegate:self];
Make sure your ViewController.h has the MKMapViewDelegate along the lines:
#interface ViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate>
Related
I'm trying to getting my current location exact according to my coordinates. I've implemented CLLocationManager in my viewController called myLocation.
My problem is, I'm getting not getting my co-ordinates for the first time, but when I again approach I got the coordinates. I'm unable to understand this problem that why this not appear for the first time.
I also tried to give a NSTimer to stoplocation but but still unable to get the result for the first time, every first time I getting a (null) value, and then getting the co-ordinates.
My Code:
#import <UIKit/UIKit.h>
#import <Corelocation/CoreLocation.h>
#interface myLocation : UITableViewController<CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocationManager *locationManager;
#end
#interface myLocation () {
CLLocationManager* _locationManager;
NSString * _lat;
NSString * _lng;
}
#end
#implementation myLocation
- (void)viewDidLoad
{
[super viewDidLoad];
_locationManager = [[CLLocationManager alloc] init];
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[_locationManager requestWhenInUseAuthorization];
[_locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];
[_locationManager stopUpdatingLocation];
_lat =[NSString stringWithFormat:#"%f",location.coordinate.latitude];
_lng =[NSString stringWithFormat:#"%f",location.coordinate.longitude];
}
- (void)viewWillAppear:(BOOL) animated
{
NSLOG(#"%#",_lat);
NSLOG(#"%#",_lng);
}
Your coordinates aren't appearing yet when you attempt to print them in viewWillAppear: because the CLLocationManager hasn't had enough time to retrieve the first location yet. Wait until didUpdateLocations: is first called before attempting to utilize the device coordinates because didUpdateLocations: is where you'll be receiving those coordinates. I recommend deleting your attempt to print the coordinates code from your viewWillAppear and simply print them in didUpdateLocations: instead.
In the comments, the OP stated he wants to "refresh" the location during viewWillAppear. I suggest stopping the updates when the view disappears and restarting the updates as soon as the view reappears:
- (void)viewWillAppear:(BOOL) animated
{
[_locationManager startUpdatingLocation];
}
- (void)viewWillDisappear:(BOOL) animated
{
[_locationManager stopUpdatingLocation];
}
It takes some time for location services to start up and call your delegate method - This almost certainly won't happen before viewWillAppear is called if you are only starting location services in viewDidLoad. Also, the first time your app executes it has to wait for the user to grant permission.
You can examine the location property of your CLLocationManager to get the most recent location. If it is nil then no location has been determined (yet).
I have been trying to move my iOS7 app with MKMapview to support iOS8. However I couldn't get the new request for users to share their locations to work properly. I create my MKMapView on a storyboard and the delegate is set and works perfectly on iOS7. Here is what I've added to support iOS8 Location sharing:
myMapView.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface myMapView : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
#property (strong, nonatomic) CLLocationManager *locationManager;
myMapView.m
//Code omitted
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
//Code omitted
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
if(IS_OS_8_OR_LATER) {
//[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
}
[self.mapView setShowsUserLocation:YES];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = self.locationManager.location.coordinate.latitude;
region.center.longitude = self.locationManager.location.coordinate.longitude;
region.span.latitudeDelta = 0.0187f;
region.span.longitudeDelta = 0.0137f;
[self.mapView setRegion:region animated:YES];
_initialPosition = NO;
}
Also I have set NSLocationAlwaysUsageDescription key and its value in my InfoPlist, which shows the correct message when prompting the user to share their location.
Unfortunately the delegate function -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations never gets called. Although each time the viewController gets loaded the [self.locationManager startUpdatingLocation] is called, but the delegate does not seem to respond to it. Is there a problem of how I set the delegate or is there something else I am missing here?
UPDATE: It seems also that my gpx file is not being called on launch. I have cleared and reloaded my location file, even changed to a default location, but no location is found: Domain=kCLErrorDomain Code=0
UPDATE 2: Here is a SS from the settings that I have actually succeeded with the user request, but fail to get/update location no matter how much I refresh.
(source: barisaltop.com)
Thanks!
I had the same problem a few days ago. The solution was adding the string keys NSLocationAlwaysUsageDescription (for [CLLocationManager requestAlwaysAuthorization]) or NSLocationWhenInUseUsageDescription (for [CLLocationManager requestWhenInUseAuthorization]) to your Supporting Files/Info.plist
You can also edit the source code of the Info.Plist with Right click > open as > Source code and add these lines:
<!-- for requestAlwaysAuthorization -->
<key>NSLocationAlwaysUsageDescription</key>
<string>Explain for what are you using the user location</string>
<!-- for requestWhenInUseAuthorization -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>Explain for what are you using the user location</string>
Hope this helps.
Finally I have succeeded to run my gpx file on the simulator. It seems that after installing Xcode 6 the first time, there might be a bug causing for gpx files to simulate. Here is how I overcame the problem:
I have deleted my app from the simulator
Under App->Capabilities enabled Background Modes->Location updates
Run the app and let it install on simulator
Allow access, and I was able to locate the user with GPX
Afterwards I disabled Location Updates.
I don't know why, but this did the trick for me.
I want to make a simple IOS 6.0 application that shows the lat/lon on screen each time the location is changed. The ViewController files are pretty trivial.
I alloc a CCLocationManager objec, set its variables and start it.
didUpdateLocations is called once or twice and then it stops being fired, even if the iPhone is moved. If I press Home button and reopen the app, the data on screen is updated once or twice before it stops again.
At simulation it works fine but not on a real 3GS iPhone.
If I uncomment the start/stop inside didUpdateLocations and continuously stop and start the service, it works, but the battery gets drained in extreme rates.
Also, this is part of a much bigger project and didUpdateLocations must be called each time the location is changed.
ViewController.h
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController
#property (nonatomic , strong) CLLocationManager *locationManager;
#property (weak, nonatomic) IBOutlet UILabel *lbl;
#end
ViewConroller.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.distanceFilter = kCLHeadingFilterNone; // whenever we move
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.PausesLocationUpdatesAutomatically = NO;
[_locationManager startUpdatingLocation];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject];
_lbl.text=[NSString stringWithFormat:#"%lf %lf",newLocation.coordinate.latitude, newLocation.coordinate.longitude];
//[_locationManager stopUpdatingLocation];
//[_locationManager startUpdatingLocation];
}
#end
If there's any advice on what's wrong, I would welcome it, I have lost a week already without solving it.
BTW, I have tested various values for the _locationManager variables but without any chnage in the behaviour
Additional info:
application is authorized to use location services
application is in foreground
Few tips for similar issues as per my experience with location-based programming on iOS.
If it works on simulator, then it should work on actual device as
well.
Unlike simulator, the actual device will only give update when its receives new information from GPS.
There are many cases where you won't get enough/required updates on a device, like when you are in a building (or covered area)
Try testing the app in open space (GPS works much better in open space rather than room/house/large-building).
PS. I don't know how much you moved but just to let you know, moving few steps doesn't make sure you will get a new location update, try using it on some vehicle (bike/car/bus).
I had this problem too — the easiest fix is simply to duplicate the target. You can then delete the original target.
It seems the problem occurs with older Xcode projects, not newly created ones.
You can add this call to your code:
locationManager.stopUpdatingLocation()
I am facing a strange problem. I am using
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations to get device location. It is working fine on simulator (iOS 6 and iOS 7) and iPhone 4S running iOS 6.1.3. It is also working fine when I connect iPhone 4 running iOS 7 to system and install the app but as soon as I unplug the device and re-run the app, this method doesn't get called and I don't get the location. How can I overcome this issue?
How did you setup you location Manager?
Do you have in your interface:
#interface ViewController : UIViewController <CLLocationManagerDelegate> {
#property (strong, nonatomic) CLLocationManager *locationManager;
}
Then in implementation in viewDidLoad:
// setup location manager
if ([CLLocationManager locationServicesEnabled]) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.delegate = self;
[_locationManager startUpdatingLocation];
}
And you need to implement the delegate:
locationManager:didUpdateLocations:
Be careful: In IOS 7
locationManager:didUpdateToLocation:fromLocation
is deprecated.
Apple link to CLLocationManagerDelegate
I'm getting an error in my IOS application. I've searched in the google and here, but the specific solution was not found!
I have a viewController called mapView that I use in two moments in my app, this view contains a MKMapView and the code.
In my mapView.h there is:
#property (strong, nonatomic) IBOutlet MKMapView *mapSpot;
And in my mapView.m there is:
- (void)viewDidLoad {
[super viewDidLoad];
[mapSpot setShowsUserLocation:YES];
}
- (void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([userLocation coordinate], 500, 500);
[mapSpot setRegion:region animated:YES];
}
So, in the first moment I load the mapView into other ViewController using:
#property (strong, nonatomic) ViewMap *mapView;
mapView = [[ViewMap alloc] initWithNibName:#"ViewMap" bundle:nil];
[self.view addSubview:[mapView view]];
I unload that ViewController and in another ViewController in other moment I load the MapView again, but in this moment the method: - (void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation not is called.
I verify if the first ViewController was unloaded and that was.
When I load the second ViewController there is a new instace of MapView, but not call the delegate method.
Anyone know something about that?
Thanks
==================================================================================
EDIT AND SOLVED:
the problem is in the way you are adding the view, in this line
[self.view addSubview:[mapView view]];
if you only add the view the controller code is not executed, instead of that you has to present the mapView, for example:
[self presentViewController:mapView animated:YES completion:nil];
The problem above, maybe happen because I'm using simulator to test app, and how the simulator not change the position map not get didUpdateUserLocation:
That's the unique explanation that I could have after the review the code, organize the classes read documentation and get error again.
Now, I'm using CLLocationManager to get position, after getting first time the position I stop it.
In the future I'll implement a system that track the user path, so using CLLocationManager is inevitable.
The mapView.m code after changes:
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *loc = [locations lastObject];
// store the location to use in any moment, it needs to be checked because the first time when get the coordinate not pass infos to load places according the current position
if (!location.latitude) {
location = [loc coordinate];
// set center the map at the current position
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 500, 500);
[mapSpotView setRegion:region animated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:#"loadPlaces" object:nil];
[locationManager stopUpdatingLocation];
}
}
if someone has a better solution, please, post here!
That's it!