1

I have tried the following code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    mapView_.myLocationEnabled = YES;

    CLLocation *myLocation = mapView_.myLocation;
    NSLog(@"%f %f",myLocation.coordinate.latitude, myLocation.coordinate.longitude);
}

The output being logged is 0.000000 0.000000.

I have also checked in the settings that the location service is enabled and is also showing enabled for my app.

Please help me figure out where am I going wrong.

  • 1
    Are you debugging on XCode or app? If XCode, are you sure you turned on simulate location in XCode on the bottom bar and set it to an actual location? If that is fine, then it doesn't look like you're using CLLocationManager. – LyricalPanda Jun 09 '14 at 01:39

2 Answers2

3

It is always show as 0, 0 in the viewDidLoad, you will have to use KVO to observe the coordinate. Try the following code:-

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.mapView addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context: nil];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.mapView removeObserver:self forKeyPath:@"myLocation"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"myLocation"] && [object isKindOfClass:[GMSMapView class]])
    {
        NSLog(@"KVO triggered. Location Latitude: %f Longitude: %f",self.mapView.myLocation.coordinate.latitude,self.mapView.myLocation.coordinate.longitude);
    }
}
Ricky
  • 10,485
  • 6
  • 36
  • 49
1

It takes time for the device and the app to work out your location. It won't be available on startup. You will need to listen for changes in the location.

This answer describes how to be notified when myLocation changes:

https://stackoverflow.com/a/15291319/148241

Community
  • 1
  • 1
Saxon Druce
  • 17,406
  • 5
  • 50
  • 71