我在应用程序中创建了一个带有地图的应用程序,并使用了一个按钮来更改为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”-section中“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 :(得分:0)
尝试获取locationManager.location
的值作为初始值,但可能是旧的,甚至是nil
。
如果您想要当前位置,则必须等待CLLocationManager
。甚至Google地图应用程序也会在显示您的位置之前等待几秒钟。
答案 1 :(得分:0)
在使用地图调用viewController之前调用locationManager。并将locationManager作为您正在初始化和推送的viewController的成员变量传递。 这是您的第一个视图,在委托中构建它。 可以吗?