【问题标题】:Can we show user location in offline in MKMapview我们可以在 MKMapview 中离线显示用户位置吗
【发布时间】:2013-11-07 07:22:13
【问题描述】:

当我们的设备离线时,我们能否获取当前用户位置“正在动画的那个蓝色球”。当我尝试获取当前用户位置并记录它时,经度和纬度都得到 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];



}

}

【问题讨论】:

  • “离线”是什么意思?根本没有互联网连接(没有 wifi 和蜂窝数据连接)或处于飞行模式?

标签: gps mkmapview cllocationmanager cllocation


【解决方案1】:

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

【讨论】:

  • 我什至没有离线获取用户定位球
  • 请确保您的位置管理器不会在您返回事件循环时自动释放,即您的位置管理器 iVar 被保留。否则,您将没有可以为您提供用户位置的位置管理器。
【解决方案2】:

这一行

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

不是个好主意。

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

只有在 viewController 被卸载时才释放 locationManager(在那里你释放其他属性)

【讨论】:

  • 以上代码运行良好,但我用 iPad mini 测试它没有 GPS
  • 运气好,离开方法时,可以在调用回调之前自动释放位置管理器。并且每次调用 getLocation() 时都会分配一个位置管理器的实例,这不是一个好主意。
猜你喜欢
  • 2011-11-14
  • 2011-08-30
  • 1970-01-01
  • 1970-01-01
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-25
相关资源
最近更新 更多