I make an App with a Map in the App and a button to change to the google.Map-App.
The code for my position is:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
float avgAccuracy = (newLocation.verticalAccuracy + newLocation.horizontalAccuracy)/2.0;
if (avgAccuracy < 500) {
[locationManager stopUpdatingLocation];
locationManager.delegate = nil;
}
storedLocation = [newLocation coordinate];
The code for the view is:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed: @"LogoNavBar.png"];
UIImageView *imageview = [[UIImageView alloc] initWithImage: image];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView: imageview];
self.navigationItem.rightBarButtonItem = button;
[imageview release];
[button release];
locationManager=[[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
mapView.showsUserLocation = YES;
NSLog(@"storedLocation-latitude für MKregion: %f", storedLocation.latitude);
MKCoordinateRegion region;
region.center.latitude = (storedLocation.latitude + 45.58058)/2.0;
region.center.longitude = (storedLocation.longitude - 0.546835)/2.0;
region.span.latitudeDelta = ABS(storedLocation.latitude - 45.58058);
region.span.longitudeDelta = ABS(storedLocation.longitude + 0.546835);
[mapView setRegion:region animated:TRUE];
MKCoordinateRegion hotel;
hotel.center.latitude = 45.58058;
hotel.center.longitude = -0.546835;
HotelPositionMarker* marker = [[HotelPositionMarker alloc] initWithCoordinate:hotel.center];
[mapView addAnnotation:marker];
[marker release];
}
My problem: when the “viewDidLoad” start, the “storedLocation” is still 0.0000 it takes a bit longer to get the own position than to start the view. The Log for the “storedLocation” in the “viewDidLoad”-section is 0 while the Log of the “storedLocation” in the “-(IBAction)” when the map is visible contains the right values.
How can I manage it to have my coordinates before the view load? I need them to center the view concerning the own position and the position given by me.
MKCoordinateRegion region;
region.center.latitude = (storedLocation.latitude + 45.58058)/2.0;
region.center.longitude = (storedLocation.longitude - 0.546835)/2.0;
region.span.latitudeDelta = ABS(storedLocation.latitude - 45.58058);
region.span.longitudeDelta = ABS(storedLocation.longitude + 0.546835);
[mapView setRegion:region animated:TRUE];
Try getting the value of
locationManager.locationfor an initial value, but it might be old or evennil.If you want the current location, you have to wait for
CLLocationManager. Even the Google Maps application waits a few seconds before showing your location.