开发者

current location too slow for view

开发者 https://www.devze.com 2022-12-26 08:51 出处:网络
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:开发者_运维技巧

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.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.


Call your locationManager before you call the viewController with the map. And pass the locationManager as member variables of the viewController you're initializing and pushing. Is that's your first view, build it in the delegate. Could it work?

0

精彩评论

暂无评论...
验证码 换一张
取 消