好的,我试图获得当您点击当前位置按钮时iPhone地图应用程序具有的相同放大/位置更新功能。我正在使用从here和here(Brad Smith发布的代码)中找到的代码来完成大部分工作。所以我真正需要做的就是围绕更新的当前位置对地图进行中心化,并在每次更新时进一步放大。在iPhone上更新/缩放到当前位置
现在我在测试centerCoord是否为null时遇到了一些困难。正如你从结果(下面列出)中看到的那样,由于某些原因我崩溃了。下面是相关代码:
- (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
我做5秒而不是30 ,因为30太长了。小蓝点出现在缩小的地图上。 5秒后,它崩溃。这里是控制台输出:2010-11-10 17:24:09.507应用名称[5731:207]显示当前位置。 Wed Nov 10 17:24:10 ...:CGImageCreateWithImageProvider:无效的图像大小:0 x 0. 2010-11-10 17:24:14.509应用名称[5731:207]检查loc控制器是否为空 – Stunner 2010-11-11 01:25:51
MapViewController。 m:55:错误:对一元感叹号的错误类型参数表示您不能对此使用感叹号。尝试CLLocationCoordinate2DIsValid(centerCoord)。 – pasine 2010-11-19 19:39:15