Search code examples
iosobjective-ccllocationmanagercurrentlocation

Current location does not show


I am trying to use GoogleMap API and apple's CLocationManager for my app, but it does not show my current location in either of them. I have set up the API in AppDelegate.m and also asking and checking user permission to track location. This is the code I have for map. I am trying to get user's current location and I will get user's destination and notify them when they are close to their location (using both estimated time and distance). I would appreciate if you can help me about that too. thanks

#import "GMapViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#import "CSMarker.h"
@import GoogleMaps;
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>


@interface GMapViewController ()  <GMSMapViewDelegate, CLLocationManagerDelegate, MKMapViewDelegate>
@property(strong, nonatomic) NSURLSession *markerSession;
@property(strong, nonatomic) GMSPolyline *polyline;
@property(strong, nonatomic) NSArray *steps;

//apple 

@property (weak, nonatomic) IBOutlet MKMapView *mapViews;

@end


@implementation GMapViewController
@synthesize viewDirection,locationManager;
@synthesize mapView;

- (void)startStandardUpdates
{
    // Create the location manager if this object does not
    // already have one.
    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self startStandardUpdates];
    self.mapView.delegate=self;
    self.mapViews.delegate=self;

    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [locationManager requestAlwaysAuthorization];
    }
    [locationManager startUpdatingLocation];



    //map type
    self.mapView.mapType = kGMSTypeNormal;
    [self.view addSubview:self.mapView];
    // to show compass and mylocation button
    self.mapView.settings.compassButton = YES;
    self.mapView.settings.myLocationButton = YES;
    //setting max and min zoom
    //[self.mapView setMinZoom:10 maxZoom:18];



    //for Drawing a line on the map
    GMSMutablePath *singleLinePath = [[GMSMutablePath alloc] init];
    // create a GMSMutablePath and add two points as lat/lng
    [singleLinePath addLatitude:28.5382 longitude:-81.3687];
    [singleLinePath addLatitude:28.5421 longitude:-81.3690];
    // use the path to create a GMSPolyline
    GMSPolyline *singleLine = [GMSPolyline polylineWithPath:singleLinePath];
    singleLine.map = self.mapView; //turn the line on

        self.mapViews.showsUserLocation = YES;
} 


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
    NSString *errorType = nil;
    if (error.code == kCLErrorDenied)
    {errorType = @"Access Decied";}
    else
    {errorType = @"Unknown Error";}
    UIAlertController* alert = [UIAlertController
                                alertControllerWithTitle: @"Alert"
                                message: @"Error getting location!"
                                preferredStyle: UIAlertControllerStyleAlert];


    UIAlertAction *okAction = [UIAlertAction
                               actionWithTitle:@"OK"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction *action)
                               {
                                   NSLog(@"OK action");

                               }];

    [alert addAction:okAction];
    [self presentViewController:alert animated:YES completion:nil];

}

@end

Solution

  • Can you check if you have defined following keys in your info.plist? All three keys are not needed, you need to define one or two of these keys depending on your uses permissions.

    NSLocationAlwaysUsageDescription: Always location description
    NSLocationWhenInUseUsageDescription: When in use location description
    NSLocationAlwaysAndWhenInUseUsageDescription: description for both