在 iPhone 上更新/放大当前位置

发布于 2024-10-01 07:27:12 字数 1871 浏览 2 评论 0原文

好吧,我正在尝试获得与 iPhone 地图应用程序相同的放大/位置更新功能,当您点击当前位置按钮时。我正在使用从此处找到的代码< /a> 和此处(代码来自 Brad Smith 的帖子)来完成大部分工作。因此,我真正需要做的就是将地图以更新的当前位置为中心,并在每次更新时进一步放大一点。

现在我在测试 centerCoord 是否为空时遇到一些困难。正如您从结果(如下所列)中看到的,它由于某种我无法弄清楚的原因而崩溃。这是相关代码:

- (void) showCurrentLocationButtonTapped {
    NSLog(@"Showing current location.");

    locController = [[LocationController alloc] init];

    [mapView setShowsUserLocation:YES];

    zoomTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(zoomToCurrentLocation) userInfo:nil repeats:YES];

}

- (void) zoomToCurrentLocation {
    CLLocationCoordinate2D centerCoord = self.locController.currentLocation.coordinate;
    NSLog(@"Checking if loc controller is null");
    if (!self.locController) {
        NSLog(@"centerCoord is null!");
    }
    NSLog(@"centerCoord: %@",centerCoord);
    [mapView setCenterCoordinate:centerCoord zoomLevel:7 animated:YES];
}

这是控制台输出:

2010-11-10 14:56:11.002 App Name[5278:207] Showing current location.
2010-11-10 14:56:11.504 App Name[5278:207] Checking if loc controller is null
2010-11-10 14:56:11.505 App Name[5278:207] centerCoord: (null)
2010-11-10 14:56:12.004 App Name[5278:207] Checking if loc controller is null
2010-11-10 14:56:12.004 App Name[5278:207] centerCoord: (null)
2010-11-10 14:56:12.504 App Name[5278:207] Checking if loc controller is null

我知道我没有测试 centerCoord 的空性,但如果我这样做:

if (!centerCoord) {

我会在该行收到错误消息:

MapViewController.m:55: error: wrong type argument to unary exclamation mark

Ok so I am trying to get the same zoom in/location update feature that the iPhone Maps application has when you tap on the current location button. I am using code I found from here and here(code from the post by Brad Smith) to do most of the work. So all I really need to do is center the map around the updated current location and zoom in a little further every time there is an update.

Right now I am having some difficulty testing for whether centerCoord is null or not. As you can see from the results(listed below) it crashes for some reason which I can't figure out. Here is the relevant code:

- (void) showCurrentLocationButtonTapped {
    NSLog(@"Showing current location.");

    locController = [[LocationController alloc] init];

    [mapView setShowsUserLocation:YES];

    zoomTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(zoomToCurrentLocation) userInfo:nil repeats:YES];

}

- (void) zoomToCurrentLocation {
    CLLocationCoordinate2D centerCoord = self.locController.currentLocation.coordinate;
    NSLog(@"Checking if loc controller is null");
    if (!self.locController) {
        NSLog(@"centerCoord is null!");
    }
    NSLog(@"centerCoord: %@",centerCoord);
    [mapView setCenterCoordinate:centerCoord zoomLevel:7 animated:YES];
}

Here is the console output:

2010-11-10 14:56:11.002 App Name[5278:207] Showing current location.
2010-11-10 14:56:11.504 App Name[5278:207] Checking if loc controller is null
2010-11-10 14:56:11.505 App Name[5278:207] centerCoord: (null)
2010-11-10 14:56:12.004 App Name[5278:207] Checking if loc controller is null
2010-11-10 14:56:12.004 App Name[5278:207] centerCoord: (null)
2010-11-10 14:56:12.504 App Name[5278:207] Checking if loc controller is null

I am aware I am not testing for the null-ness of centerCoord, but if I do this:

if (!centerCoord) {

I get an error on that line saying:

MapViewController.m:55: error: wrong type argument to unary exclamation mark

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

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

发布评论

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

评论(2

绿光 2024-10-08 07:27:12

您需要执行以下操作:

首先,检查位置服务是否已启用:

if ([CLLocationManager locationServicesEnabled])
    [self findUserLocation];

然后在 findUserLocation 方法中,实例化 CLLocationManager 并开始接收更新:

- (void)findUserLocation
{
    CLLocationManager *locationManager_ = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracy{ChooseYourOptionHere};
    [locationManager startUpdatingLocation];
}

最后,实现此委托方法,每次收到更新时都会调用该方法。根据您想要的精度(上面设置)检查当前位置的精度,如果您满意,请将地图居中,并且不要忘记阻止位置管理器接收更新:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if ([newLocation horizontalAccuracy] < [manager desiredAccuracy])
    {
        // Accuracy is good enough, zoom to current location
        [manager stopUpdatingLocation];
    }
// else keep trying...
}

Here's what you need to do:

First, check if location services is enabled:

if ([CLLocationManager locationServicesEnabled])
    [self findUserLocation];

Then in your findUserLocation method, instantiate your CLLocationManager and start receiving updates:

- (void)findUserLocation
{
    CLLocationManager *locationManager_ = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracy{ChooseYourOptionHere};
    [locationManager startUpdatingLocation];
}

And finally, implement this delegate method, which will get called every time an update is received. Check the current location's accuracy against your desired accuracy (set above), center the map if you are happy with it AND don't forget to stop the location manager from receiving updates:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if ([newLocation horizontalAccuracy] < [manager desiredAccuracy])
    {
        // Accuracy is good enough, zoom to current location
        [manager stopUpdatingLocation];
    }
// else keep trying...
}
夜光 2024-10-08 07:27:12

也许位置控制器需要一些时间来更新您的位置。
尝试将计时器设置为 30 秒,看看会发生什么。

Maybe the Location Controller is taking time to update your location.
Try to set your timer to 30 seconds and see what happens.

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