好的,我正在尝试获取与点击当前位置按钮时iPhone地图应用程序所具有的相同的放大/位置更新功能。我正在使用我从here和here( 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发布于 2011-03-22 15:48:57
下面是你需要做的:
首先,检查是否开启了定位服务:
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...
}发布于 2010-11-11 08:12:25
也许位置控制器正在花时间更新您的位置。
试着把你的计时器设置到30秒,看看会发生什么。
https://stackoverflow.com/questions/4150035
复制相似问题