MKMapView mathod 'didUpdateUserLocation' not calling - ios

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.

Related

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.

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

Show user's location Xcode6 /IOS8

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.

CLLLocationManager inside NSObject

I am trying to create a NSObject that will get the location and the address, I will also be running some other code. I want to just throw it into my different apps. I am having issues with it updating the location. I have the protocol and delegate working, but I will post all my code to try to make it clear.
In my getLocation.h
#protocol getLocationDelegate
- (void)getLocation:(NSString *)address getLongitude:(CGFloat)longitude getLatitude:(CGFloat)latitude;
#end
#interface getLocation : NSObject <CLLocationManagerDelegate> {
CGFloat latitude;
CGFloat longitude;
NSString *address;
}
#property (nonatomic, strong) id<getLocationDelegate> delegate;
#property (nonatomic, retain) CLLocationManager *locationManager;
- (void)getLocationInfo;
In my getLocation.m
- (id)init
{
if (self = [super init]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
return self;
}
- (void)getLocationInfo {
[self.locationManager startUpdatingLocation];
// This is being called but not starting the locationManager
}
After I get the address and the location I call the protocol like this, in my getLocation.h
[self.delegate getLocation:address getLongitude:longitude getLatitude:latitude];
In my .m file that I want to get the location this is the code
getLocation *location = [[getLocation alloc] init];
[location setDelegate:self];
[location getLocationInfo];
Then I have my method when the delegate gets called
-(void)getLocation:(NSString *)address getLongitude:(CGFloat)longitude getLatitude:(CGFloat)latitude {
// Do code after location and address has been returned
}
Now my protocol and delegate are working what my problem is I can't get the locationManager to start updating. Not sure whats going on.
I figured it out, ARC was releasing getLocation *location = [[getLocation alloc] init]; before CoreLocation was starting. So I just needed to declare it in my .h and have it be strong.

initialization and allocation without arc?

Sorry its a basic question, I want to know why my code doesn't need alloc/init for mapView. Does it happen automatically on retain ? I am not using ARC and on alloc/init of my MKMapView* mapView it does not result in error but the map view doesn't show the location information and also doesn't appear as hybrid type....but on removing the alloc/init statement fro viewDidLoad it works all fine !! why?
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#interface MDViewController : UIViewController<CLLocationManagerDelegate, MKMapViewDelegate>
#property (retain, nonatomic) IBOutlet MKMapView* mapView;
#end
-----
#import "MDViewController.h"
#interface MDViewController ()
{
CLLocationManager* lmanager;
}
#end
#implementation MDViewController
#synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
lmanager= [[CLLocationManager alloc]init];
lmanager.delegate=self;
lmanager.desiredAccuracy=kCLLocationAccuracyBest;
lmanager.distanceFilter=kCLDistanceFilterNone;
[lmanager startUpdatingLocation];
//mapView = [[MKMapView alloc]init];//without allocating here it works
mapView.delegate=self;
mapView.mapType=MKMapTypeHybrid;
}
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//update map
MKCoordinateSpan span;
span.latitudeDelta= .001;
span.longitudeDelta=.001;
MKCoordinateRegion region;
region.center= newLocation.coordinate;
region.span=span;
[mapView setRegion:region animated:YES];
[mapView setShowsUserLocation:YES];
}
You don't need to alloc init your mapview because it is done for you by the Xib. When loading the interface xib, the frameworks see you have a frozen mapview and alloc init it automagically, then assign that mapview to the one in your viewcontroller code.
If in your code you alloc init, you break the link between the two.
One way to make it work is to have no mapview in your IB xib and alloc init it, setDelegate, set frame and finally add its view as a subview of the main view.
I try to stay concise. I hope it us clear to you. And ARC has no relation whatsoever with all that.

Resources