我们可以在MKMapview中离线显示用户位置吗?

时间:2013-11-07 07:22:14

标签: gps mkmapview cllocationmanager cllocation

当我们的设备离线时,我们可以获得当前用户位置“那个动画的蓝色球”。当我试图获取当前用户位置并记录它时,经度和纬度都得到0.00000这里是我的代码用于获取当前用户位置。我正在使用ipad mini进行测试。我还在.h文件中添加了CLLocationManagerDelegate。

- (void)viewDidLoad {
     [self getUserLocation];
      self.myMapView.showsUserLocation = YES;

 }
-(void)getUserLocation{
if ([CLLocationManager locationServicesEnabled]) {
    locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    [locationManager startUpdatingLocation];
}else{
    NSLog(@"User location Disabled");
}


}

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
 {
CLLocation *currentGPSLocation = newLocation;

if (currentGPSLocation != nil) {
    currentLocation.latitude = self.myMapView.userLocation.coordinate.latitude;
    currentLocation.longitude = self.myMapView.userLocation.coordinate.longitude;

    currentLocationWalk.latitude=self.myMapView.userLocation.coordinate.latitude;
    currentLocationWalk.longitude=self.myMapView.userLocation.coordinate.longitude;
    NSLog(@"didUpdateToLocation: %f,%f", self.myMapView.userLocation.coordinate.latitude,self.myMapView.userLocation.coordinate.longitude);
    statusLabel.textColor = [UIColor blackColor];
    statusLabel.text = [NSString stringWithFormat:@"%f",self.myMapView.userLocation.coordinate.latitude];
    statusLabel1.text = [NSString stringWithFormat:@"%f",self.myMapView.userLocation.coordinate.longitude];



}

}

2 个答案:

答案 0 :(得分:0)

您没有使用ARC(我强烈建议您使用它,它可以避免大多数内存管理错误!) 因此,“locationManager”可能是未保留的实例变量(可能您没有将其声明为保留属性)。
在这种情况下,当您的代码返回主运行循环时,它可能已经被释放,其中自动释放池被耗尽。

答案 1 :(得分:0)

这一行

locationManager = [[[CLLocationManager alloc] init] autorelease];

不是个好主意。

将locationManager定义为属性, 在viewDidLoad()中初始化locationManager,但删除自动释放!

并仅在卸载viewController时释放locationManager(在那里释放其他属性)