views:

177

answers:

1

Hi,

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];
A: 

Try getting the value of locationManager.location for an initial value, but it might be old or even nil.

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.

Can Berk Güder
Is is nil :(But how can I manage to wait? Does there a code exists to implement in the viewDidLoad to wait 2 seconds?
Christian
I meant wait for `didUpdateToLocation:` to be called. Do the rest of the stuff in `viewDidLoad`, and for example show a spinning activity indicator until `didUpdateToLocation:` is called for the first time.
Can Berk Güder
I tried without any success. I changed from viewDidLoad to viewWillAppear. In this case, when you go back to the menu and call the view again within a second the current position will be displayed correct. But that's not the solution....
Christian
It appears I haven't made myself clear. Do you *have* to do whatever you're doing (centering the view?) in `viewDidLoad` or `viewDidAppear`? Just do it in `didUpdateToLocation:`.
Can Berk Güder