Iphone 在加载地图之前查找用户位置

Iphone 在加载地图之前查找用户位置,iphone,cocoa-touch,mapkit,Iphone,Cocoa Touch,Mapkit,我正在尝试使用以下方法查找用户的位置: CLLocationCoordinate2D _location; CLLocation *userLoc = nil; if(appDelegate.clLocationManager.locationServicesEnabled){ userLoc = mapView.userLocation.location; _location = userLoc.coordinate; } 问题是我还没有初始化映射,所以上面的操作不起作用。有

我正在尝试使用以下方法查找用户的位置:

CLLocationCoordinate2D _location;
CLLocation *userLoc = nil;
if(appDelegate.clLocationManager.locationServicesEnabled){
    userLoc = mapView.userLocation.location;
    _location = userLoc.coordinate;
}

问题是我还没有初始化映射,所以上面的操作不起作用。有没有不使用mapkit获取用户位置的方法?或者我应该使用另一种方法吗?

我差点删除了这个问题,但看到有人投了弃权票。这似乎是答案:

CLLocationCoordinate2D _location;
CLLocation *userLoc = nil;
if(appDelegate.clLocationManager.locationServicesEnabled){
    userLoc = appDelegate.clLocationManager.location;
    _location = userLoc.coordinate;
}

appDelegate只是对我的location manager实例所在的application delegate的引用。

我差点删除了这个问题,但看到有人投了赞成票。这似乎是答案:

CLLocationCoordinate2D _location;
CLLocation *userLoc = nil;
if(appDelegate.clLocationManager.locationServicesEnabled){
    userLoc = appDelegate.clLocationManager.location;
    _location = userLoc.coordinate;
}

appDelegate只是对我的location manager实例所在的应用程序委托的引用。

要获取用户的位置,您需要在尝试读取位置信息之前实际“启动”location manager。我建议你们看看苹果的LocateMe样品

简而言之,获取用户的位置信息可以通过两个步骤完成:

- (void)viewDidLoad {
    [[self locationManager] startUpdatingLocation];
}

- (CLLocationManager *)locationManager { 

    if (locationManager != nil) {       
        return locationManager; 
    }

    NSLog(@"%s, Creating new Location Manager instance.", __FUNCTION__);
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = DISTANCE_FILTER_VALUE;
    locationManager.delegate = self; 

    return locationManager; 
}
。。。然后呢,

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    NSLog(@"%s, location manager returned a new location.", __FUNCTION__);

    // Check if location is valid (or good enough for us), 
    // and then process the location
    if ([self locationIsValid:newLocation]) {
            [self processLocation:newLocation];

            // Very important! When your done with it, stop the location manager
            [[self locationManager] sopUpdatingLocation];

            // (Maybe even) clear it
            locationManager.delegate = nil;
            [locationManager release];
            locationManager = nil;
    }
}

希望这有帮助

要获取用户的位置,您需要在尝试读取位置信息之前实际“启动”位置管理器。我建议你们看看苹果的LocateMe样品

简而言之,获取用户的位置信息可以通过两个步骤完成:

- (void)viewDidLoad {
    [[self locationManager] startUpdatingLocation];
}

- (CLLocationManager *)locationManager { 

    if (locationManager != nil) {       
        return locationManager; 
    }

    NSLog(@"%s, Creating new Location Manager instance.", __FUNCTION__);
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = DISTANCE_FILTER_VALUE;
    locationManager.delegate = self; 

    return locationManager; 
}
。。。然后呢,

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    NSLog(@"%s, location manager returned a new location.", __FUNCTION__);

    // Check if location is valid (or good enough for us), 
    // and then process the location
    if ([self locationIsValid:newLocation]) {
            [self processLocation:newLocation];

            // Very important! When your done with it, stop the location manager
            [[self locationManager] sopUpdatingLocation];

            // (Maybe even) clear it
            locationManager.delegate = nil;
            [locationManager release];
            locationManager = nil;
    }
}
希望这有帮助