【问题标题】:current location too slow for view当前位置太慢,无法查看
【发布时间】:2010-04-06 10:42:40
【问题描述】:

我制作了一个App,在App里面有一个地图,还有一个按钮可以改成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”的 Log 为 0,而“-(IBAction)”中的“storedLocation”的 Log 包含正确的值。

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

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

【问题讨论】:

    标签: iphone xcode iphone-sdk-3.0 map


    【解决方案1】:

    尝试获取 locationManager.location 的值作为初始值,但它可能是旧的,甚至可能是 nil

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

    【讨论】:

    • Is is nil :( 但是我怎么能等待?是否有代码可以在 viewDidLoad 中实现等待 2 秒?
    • 我的意思是等待didUpdateToLocation: 被调用。在viewDidLoad 中做其余的事情,例如在第一次调用didUpdateToLocation: 之前显示一个旋转的活动指示器。
    • 我试过没有成功。我从 viewDidLoad 更改为 viewWillAppear。在这种情况下,当您返回菜单并在一秒钟内再次调用视图时,当前位置将正确显示。但这不是解决方案......
    • 看来我还没有说清楚。您是否必须viewDidLoadviewDidAppear 中做任何您正在做的事情(使视图居中?)?只需在didUpdateToLocation: 中进行操作即可。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      相关资源
      最近更新 更多