I am trying to get my current location using cllocationmanager.
Problem:
The delegate method
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
always runs after the viewdidappear. I startupdatingLocation in the viewDidLoad. And call a method SetMapView at the end of the viewdidLoad .
-(void)setMapView{
MKCoordinateRegion zoomIn = plotsMapView.region;
zoomIn.span.latitudeDelta = 0.2;
zoomIn.span.longitudeDelta = 0.2;
zoomIn.center.latitude = self.currentLocation.coordinate.latitude;//[[[m_CityMarkerArray objectAtIndex:0] valueForKey:@"lat"] doubleValue];
zoomIn.center.longitude = self.currentLocation.coordinate.longitude;//[[[m_CityMarkerArray objectAtIndex:0] valueForKey:@"lng"] doubleValue];
NSLog(@"%f LAT",self.currentLocation.coordinate.latitude);
[plotsMapView setRegion:zoomIn animated:YES];
}
the LAT is returned 0.0000.
BUT if I call the same method in viewDidAppear it works fine.
And I need it in the viewdidload because everytime I come back to view.I reloads the map to the base location if I call the method in viewDidAppear.
Please Help!
Thanks!
Call your setMapView method inside didUpdateToLocation method, provided you set your location manager delegate to self and added CLLocationManagerDelegate in your .h file. Inside your didUpdateToLocation, before calling your setMapView method, assign the newLocation coordinates to your currentLocation coordinates.