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

发布于 2024-08-24 18:39:31 字数 331 浏览 5 评论 0原文

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

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

问题是我尚未初始化地图,因此上述方法不起作用。有没有一种方法可以在不使用mapkit的情况下获取用户的位置?或者我应该使用不同的技术?

I'm trying to find the user's location using:

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

The problem is that I haven't yet initialized the map so the above doesn't work. Is there a way to get the user's location without using mapkit? Or should I be using a different technique?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哭了丶谁疼 2024-08-31 18:39:32

要获取用户的位置,您需要在尝试读取位置信息之前实际“启动”位置管理器。我建议您查看 Apple 的 LocateMe 示例。

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

希望这有帮助!

To get the user's location you need to actually "start" the location manager before trying to read the Location information. I suggest that you look at LocateMe sample from Apple.

LocateMe Sample Code

In a nutshell, getting user's location information can be done in two steps:

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

... and then,

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

Hope this helps!

稍尽春風 2024-08-31 18:39:32

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

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

appDelegate 只是对应用程序委托的引用,我的位置管理器实例位于其中。

I almost deleted this question but see somebody gave it an up vote. This seems to be the answer:

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

appDelegate is just a reference to the application delegate, where my location manager instance lives.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文