Show user's location Xcode6 /IOS8 - geolocation

I've updated to Xcode 6 (from Xcode 5) and now my app is not working anymore (I was quite proud it worked under IOS7). I have this "famous" debug output:
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
Of course I've been googling this message to find a solution but it seems that nothing's working. So I am asking for your advice.
Here is my header file:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MapPoint.h"
#define kGOOGLE_API_KEY #"my google api"
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#interface XYZViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
CLLocationCoordinate2D currentCentre;
int currenDist;
BOOL firstLaunch;
}
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#end
And here is my implementation file:
#import "XYZViewController.h"
#import "DetailViewController.h"
#interface XYZViewController ()
#end
#implementation XYZViewController
-(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Make this controller the delegate for the map view.
self.mapView.delegate = self;
// Ensure that you can view your own location in the map view.
[self.mapView setShowsUserLocation:YES];
//Instantiate a location object.
locationManager = [[CLLocationManager alloc] init];
//Make this controller the delegate for the location manager.
[locationManager setDelegate:self];
//Set some parameters for the location object.
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
firstLaunch=YES;
}

You need to do as the error description tell you, and add a call to requestWhenInUseAuthorization or requestAlwaysAuthorization. This has been covered before, ie. iOS alertview for location permission doesn't pop up
The required changes could look like this:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Make this controller the delegate for the map view.
self.mapView.delegate = self;
//Instantiate a location object.
locationManager = [[CLLocationManager alloc] init];
//Make this controller the delegate for the location manager.
[locationManager setDelegate:self];
//Set some parameters for the location object.
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
// Request use on iOS 8
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
// Ensure that you can view your own location in the map view.
[self.mapView setShowsUserLocation:YES];
} else {
[locationManager requestWhenInUseAuthorization];
}
firstLaunch=YES;
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
// Ensure that you can view your own location in the map view.
[self.mapView setShowsUserLocation:YES];
}
}
The above code do a version check (in the apple recommended way), you could also do a feature check, which is cleaner, but sometime cause problems in my own experience.
The tricky part is you need to provide a description of your use in the app Info.plist. Else the UIAlert will not appear and you will not be given permission. Go to the project/target settings in xcode, click the "Info" tab. Click the + button. For the key enter NSLocationWhenInUseUsageDescription and the value should be a string describing how you will use the user's location. You can also open the 'Info.plist' file under 'Supporting Files'.
I suspect this is why it hasn't worked for you yet.

Related

The current location is not visible on the map using MapKit

I want to show the user's current location on map using MapKit and everything goes ok but the user current location is not visible on the maps.
Here are the properties I have set for mapView:
Here is rest of the controller's code:
#import <MapKit/MapKit.h>
#import "ShowMarkerAndNavigateVC.h"
#interface ShowMarkerAndNavigateVC ()
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#end
CLLocationManager *mgr;
#implementation ShowMarkerAndNavigateVC
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"View loaded Successfully");
mgr = [[CLLocationManager alloc] init];
[mgr requestWhenInUseAuthorization];
NSLog(#"Reuquested always authorization");
}
- (IBAction)backButtonPressed:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
#end
I have also set the mapView delegate to the current controller as shown here:
The expected result is that it should show the user's current location.
The actual result is that it does not show location on the map as shown here:
First, I would suggest actually setting showsUserLocation property to YES in code.
Second, note that it is up to you to show the region where the user location actually is; for example:
- (void)mapView:(MKMapView *)mapView
didUpdateUserLocation:(MKUserLocation *)userLocation {
CLLocationCoordinate2D coordinate = userLocation.location.coordinate;
MKCoordinateRegion reg =
MKCoordinateRegionMakeWithDistance(coordinate, 600, 600);
mapView.region = reg;
}
If you're using the simulator, you won't get the user's location, because the simulator doesn’t know its own location.
To do so, open the Debug menu of the Simulator and select Apple in the ***Location* sub-menu.
Hope this will help you.

MKMapView mathod 'didUpdateUserLocation' not calling

I have this code in my file,
#synthesize myLocation;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationManager = [[CLLocationManager alloc] init];
myLocation.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
[myLocation setShowsUserLocation: YES];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.myLocation setRegion:[self.myLocation regionThatFits:region] animated:YES];
}
But, method didUpdateUserLocation not calling.
I already added required frameworks in app.
I added everything in info.plist file. But still it is not calling.
This is my .h file,
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#interface MapView : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
#property (weak, nonatomic) IBOutlet MKMapView *myLocation;
#property(nonatomic, retain) CLLocationManager *locationManager;
#end
I just solved the problem,
locationManager = [[CLLocationManager alloc] init];
myLocation.delegate = self;
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
[myLocation setShowsUserLocation: YES];
Use setShowUserLocation with value YES after startUpdatingLocation
You have not called the method to update the location yet so the delegate method is not getting called. You need to call method for startUpdatingLocation.
For Objective - C:
[locationManager startUpdatingLocation];
For Swift:
locationManager.startUpdatingLocation()
Brother I tried your code.The delegate method is not called.
Then I found out the solution you did not add below line at the end of the viewDidLoad method. Please add
[self.locationManager startUpdatingLocation];
After I added above line code in viewDidLoad method,the delegate method called successfully.

Cannot get the my location dot for Google Maps iOS SDK

Currently, I am not able to get my location in the app I am developing.
Basically, in the documentation for Google Maps iOS SDK, Google mentions that you can:
enable the blue "My Location" dot and compass direction by setting
myLocationEnabled on GMSMapView
https://developers.google.com/maps/documentation/ios/map#my_location
However, when I do that, the dot does not appear in my view.
Here's my setup, I have a view controller that contains a view. This view contains three things, two buttons and a map view:
I have linked my mapView with GMSMapView and I can see the map without any problems.
Now, what I would want, is to be located.
I tried two things. First, using a custom, draft button (the i for information), I tried to manually set the location to the mapView but this wasn't working even though the locationServicesEnabled method returned YES.
Then, I tried using GoogleMaps's dot but this isn't working either.
Here's my code:
StudyDisplayViewController.h:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "GoogleMaps/GoogleMaps.h"
#interface StudyDisplayViewController : UIViewController <CLLocationManagerDelegate> {
}
#property (strong, readwrite) CLLocationManager *locationManager;
#property (weak, nonatomic) IBOutlet GMSMapView *mapView;
#end
StudyDisplayViewController.m
#import "StudyDisplayViewController.h"
#interface StudyDisplayViewController ()
- (IBAction)closeStudyDisplay:(id)sender;
- (IBAction)locateMe:(id)sender;
#end
#implementation StudyDisplayViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
NSLog(#"Starting the location service");
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startMonitoringSignificantLocationChanges];
if([CLLocationManager locationServicesEnabled]) {
NSLog(#"all good");
}
else {
NSLog(#"Damn son");
}
}
self.mapView.settings.compassButton = YES;
self.mapView.myLocationEnabled = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
}
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSLog(#"Getting Location");
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)closeStudyDisplay:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)locateMe:(id)sender {
NSLog(#"User's location: %#", self.mapView.myLocation);
}
I am using Xcode 6.3.2 and iOS 8.3.
First check out following things are there or not.
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate= self;
if([locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]){
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
_mapView.myLocationEnabled = YES;
_mapView.settings.myLocationButton = YES;
If Everything is there and if you are testing on Simulator then try to change the location in Simulator from debug Tab, e.g. change It to Free car run of any of them.
Also, check if you have added Description in PLIST for NSLocationWhenInUseUsageDescription.

User Location Will Not display on mapView

I am creating a simple UIMapView with custom annotations. Just upgraded to Yosemite and Xcode 6.1.1. I have tried everything to get user location to show on map but for some reason the blue dot doesn't show on the map. I have selected show user location in the inspector for the UIMapView. The error listed at the end shows in the log window at the bottom of the screen when I run it on my phone. Thank you for any help.
My .h file
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController <MKMapViewDelegate>
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
My .m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.mapView.delegate self];
[self.mapView setShowsUserLocation:YES];
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocationCoordinate2D loc = [userLocation coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 500, 500);
[self.mapView setRegion:region animated:YES];
}
ERROR RECEIVED AFTER RUNNING
501:107687] Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
As per my comments on the question... you are in a Permission error situation.
You need t check for User Permission before you call the update methods...
CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];
if (authorizationStatus == kCLAuthorizationStatusAuthorized ||
authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {
// update location here...
}
Then, as pointed out by #zaheer, you need to add a Key-Value par to the -Info.plist
NSLocationWhenInUseUsageDescription
Some Message To The User About Why You Want Their Location
or
NSLocationAlwaysUsageDescription
Some Witty Comment About The NSA
depending upon whether you need the location only when the app is in the foreground, or in the background...

ios get current location and show blue dot using location service

what i need for my app is, to display my current location on my mapview by using location service,
here is my code
.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface MapDirectoryViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
#property (weak, nonatomic) IBOutlet MKMapView *myMapView;
- (IBAction)backAction:(id)sender;
#end
.m
#interface MapDirectoryViewController ()
#end
#implementation MapDirectoryViewController
{
MKCoordinateRegion myRegion;
CLLocationCoordinate2D center;
MKCoordinateSpan span;
NSMutableArray *locations;
CLLocationCoordinate2D location;
Annotation *myAnnoation;
}
#synthesize myMapView;
- (void)viewDidLoad {
[super viewDidLoad];
myMapView.showsUserLocation = YES;
[myMapView setShowsUserLocation:YES];
[myMapView setCenterCoordinate:myMapView.userLocation.location.coordinate animated:YES];
}
#delegation
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
//what do i need to do in here?
}
what i need excatly is to get blue dot to show up on map and make the dot as center of the map when app loads,
as you can see in my viewdidload, i tried many way to active the user current location, but none of them works...
and i realise one more thing in my actual iphone6, under general, my app doesnt have 'location' option there so i cant phycially go to active it.... e.g google map or facebook app they all have 'location' option under general app config...
any advise? any code example would be really helpful, thank you very much.
If you set location permission i.e.
[LocationManager requestWhenInUseAuthorization]
or
[LocationManager requestAlwaysAuthorization]
then set
[self.mapLocation setShowsUserLocation:YES];
and also write code in map annotation delegate like this:
- (MKAnnotationView *)mapView:(MKMapView *)mapViewIn viewForAnnotation:(id <MKAnnotation>)annotation {
//Your stuff
if (annotation == _mapLocation.userLocation) {
return nil;
}
}
solution:
1, 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>
2,
- (void)viewDidLoad {
[super viewDidLoad];
serviceConnector = [[ServiceConnector alloc] init];
serviceConnector.delegate = self;
[serviceConnector retrieveDataFromWebservice:ALL_MOSQUE_ADDRESS_JSON_WS];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
[myMapView setShowsUserLocation:YES];
[myMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
[myMapView setCenterCoordinate:myMapView.userLocation.location.coordinate animated:YES];
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(#"location delegate called");
}
now it get me the current location with blue dot , and calling deleagation method correctly in ios 8

Resources