【问题标题】:Updating/Zooming Into Current Location on iPhone在 iPhone 上更新/放大到当前位置
【发布时间】:2010-11-10 23:15:39
【问题描述】:

好的,我正在尝试获得与 iPhone 地图应用程序在您点击当前位置按钮时相同的放大/位置更新功能。我正在使用从herehere(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

【问题讨论】:

    标签: iphone mkmapview


    【解决方案1】:

    这是你需要做的:

    首先,检查定位服务是否开启:

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

    【讨论】:

    • 我才意识到这就像 6 个月大,不知道为什么它又被撞到了顶部?
    • 我不知道,呵呵。不过,感谢您的回答,我将回到我项目的那一部分并按照您的建议做,如果它有助于我做我正在寻找的事情,我一定会接受您的回答。 :) 无论如何,你都得到了我的 +1 回答。
    【解决方案2】:

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

    【讨论】:

    • 我做了 5 秒而不是 30 秒,因为 30 太长了。小蓝点显示在缩小的地图上。 5秒后,它崩溃了。这是控制台输出:2010-11-10 17:24:09.507 App Name[5731:207] 显示当前位置。 11 月 10 日星期三 17:24:10 ...:CGImageCreateWithImageProvider:无效图像大小:0 x 0。2010-11-10 17:24:14.509 应用程序名称 [5731:207] 检查 loc 控制器是否为空
    • MapViewController.m:55:错误:一元感叹号的类型参数错误意味着您不能为此使用感叹号。试试 CLLocationCoordinate2DIsValid(centerCoord)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2011-11-21
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    相关资源
    最近更新 更多