2010-04-06 48 views
0

我使用应用中的地图制作应用,并使用按钮更改为google.Map-App。当前位置视图太慢

我的位置的代码是:

-(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]; 

为视图的代码是:

- (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]; 
} 

我的问题:当 “viewDidLoad中” 启动时, “storedLocation” 仍然是0.0000它需要更长的时间才能获得自己的位置,而不是启动视图。 “viewDidLoad”部分中的“storedLocation”的日志为0,而当地图可见时“ - (IBAction)”中的“storedLocation”的日志包含正确的值。

如何在视图加载之前管理它以获得我的坐标?我需要他们集中关于自己的立场和我给出的立场的观点。

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

回答

0

尝试获得的locationManager.location值的初始值,但也可能是旧的,甚至nil

如果您想要当前位置,则必须等待CLLocationManager。即使Google地图应用程序在显示您的位置之前也会等待几秒钟。

+0

是为零:( 但我怎么能管理等是否有一个代码存在于viewDidLoad中实施,以间隔2秒 – Christian 2010-04-06 13:15:39

+0

我的意思是等待'didUpdateToLocation:'被调用执行的其他在'viewDidLoad'中的东西,例如显示一个旋转活动指示器,直到第一次调用'didUpdateToLocation:' – 2010-04-06 13:19:38

+0

我试过没有任何成功,我从viewDidLoad改成了viewWillAppear,在这种情况下,当你回到菜单并在一秒钟内再次调用视图,当前位置将显示正确。但这不是解决方案.... – Christian 2010-04-06 18:27:16

0

在您使用地图调用viewController之前调用您的locationManager。并传递locationManager作为您正在初始化和推送的viewController的成员变量。 这是你的第一个观点,在委托中构建它。 它可以工作吗? ?