【问题标题】:CLLocationManager startUpdatingLocation not workingCLLocationManager startUpdatingLocation 不起作用
【发布时间】:2012-07-26 17:13:51
【问题描述】:

所以现在我至少可以使用以下代码获得回调...

- (void)viewDidLoad {

[super viewDidLoad];
mapView=[[MKMapView alloc] initWithFrame:self.view.frame];
//mapView.showsUserLocation=TRUE;
mapView.delegate=self;
[self.view insertSubview:mapView atIndex:0];

NSLog(@"locationServicesEnabled: %@", [CLLocationManager locationServicesEnabled] ? @"YES":@"NO");
    CLLocationManager *newLocationManager = [[CLLocationManager alloc] init];
    [newLocationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [newLocationManager setDistanceFilter:kCLDistanceFilterNone];
    [self setLocationManager:newLocationManager];


[[self locationManager] setDelegate:self];
[[self locationManager] startUpdatingLocation];
NSLog(@"Started updating Location");

}


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

NSLog(@"Did update to location");
mStoreLocationButton.hidden=FALSE;
location=newLocation.coordinate;

MKCoordinateRegion region;
region.center=location;
MKCoordinateSpan span;
span.latitudeDelta=0.01;
span.longitudeDelta=0.01;
region.span=span;


[mapView setRegion:region animated:TRUE];


}

我可以在第二种方法中设置断点,并且 NSLog 正在报告持续的位置更新,但由于某种原因,使用 span 的缩放不起作用。知道为什么吗?它有我的坐标和一切。对这个有点摸不着头脑。

【问题讨论】:

  • 你的代码似乎没问题..问题出在其他地方
  • 您是否在模拟器中遇到了这些问题?是否启用了位置模拟?
  • 是的,模拟器有问题。我已启用模拟位置。也许我需要在设备上进行测试。

标签: iphone ios mkmapview zooming cllocationmanager


【解决方案1】:

嗯,首先,您可以永远确保位置管理器能够在第一时间更新位置。更新期间可能出现错误,或者您无权访问用户的位置。

实现这个 CLLocationManager 委托方法并验证错误。

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

“此方法的实现是可选的。但是,您应该实现此方法。”

【讨论】:

  • 不报告任何错误。我正在 iOS 模拟器上尝试这个测试。这和它有什么关系吗?
【解决方案2】:

我认为,您可以通过两种方式完成这项工作:

  1. 使用 CLLocation 框架

检查一下,您已经采用了带有 CLLocationManagerDelegate 方法的 ViEWController

#import<MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate, 
                                              MKMapViewDelegate>
{
    CLLocationCoordinate2D location;
    MKMapView *mapView;
}
@end

在 ViewController.m 中:

@implementation GSViewController
- (void)viewDidLoad {

    [super viewDidLoad];
    mapView=[[MKMapView alloc] initWithFrame:self.view.frame];
    mapView.showsUserLocation=TRUE;
    mapView.delegate=self;
    [self.view insertSubview:mapView atIndex:0];


    CLLocationManager *locationManager=[[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;

    [locationManager startUpdatingLocation];

}

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


    NSLog(@"new location: %@", newLocation);
    location=newLocation.coordinate;

    MKCoordinateRegion region;
    region.center=location;
    MKCoordinateSpan span;
    span.latitudeDelta=0.01;
    span.longitudeDelta=0.01;
    region.span=span;

    [mapView setRegion:region animated:TRUE];

}


-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"error: %@", error.description);
}
@end

2.使用相同的MKMapKit框架 您可以使用名为 didUpdateUserLocation 的 MKMapViewDelegate 方法来执行此操作: 在这里你不需要 CLLocaionManager, 这将通过以下方式完成: 在 ViewController.h 中:

#import <MapKit/MapKit.h>
@interface ViewController : UIViewController < MKMapViewDelegate>
{
    CLLocationCoordinate2D location;
    MKMapView *mapView;
}
@end

在 ViewController.m 文件中:

@implementation GSViewController
- (void)viewDidLoad {

    [super viewDidLoad];
    mapView=[[MKMapView alloc] initWithFrame:self.view.frame];
    mapView.showsUserLocation=TRUE;
    mapView.delegate=self;
    [self.view insertSubview:mapView atIndex:0];

}

-(void)mapView:(MKMapView *)mapV didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"map new location: %f %f", userLocation.coordinate.latitude, userLocation.coordinate.longitude);
    location=userLocation.coordinate;

    MKCoordinateRegion region;
    region.center=location;
    MKCoordinateSpan span;
    span.latitudeDelta=0.1;
    span.longitudeDelta=0.1;
    region.span=span;

    [mapV setRegion:region animated:TRUE];
}
@end

【讨论】:

  • 使用方法 1,我收到回调,但缩放(跨度)没有缩放到位置。我修改了我的代码。也许看看有什么问题?
【解决方案3】:

确保您已在@interface 文件中添加&lt;CLLocationManagerDelegate&gt;

编辑

如果代理设置正确,请确保您使用的是 locationManager 属性:

.h 文件中:

@property (nonatomic, strong) CLLocationManager *locationManager;

viewDidLoad:

self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager startUpdatingLocation];

【讨论】:

  • 这只会修复警告,不会有其他问题。
【解决方案4】:

如果您仅在模拟器中运行此程序,您可能需要提示它更改坐标。在 Xcode 中,调试输出窗格上方有一个带有典型位置服务箭头的栏。旁边是位置的下拉列表。一旦您的应用程序运行,切换它正在模拟的位置并查看该更改是否会触发您的代码。然后在真机上进行测试。

【讨论】:

  • 这很聪明。不幸的是它没有用,但我喜欢这个主意。我只需要在真实设备上进行测试。
【解决方案5】:

将 CLLocationManager 分配给您班级的(强)属性。 (我假设您使用的是 ARC BTW。)现在 CLLocationManager 不会超过 viewDidLoad 方法的末尾,因此它也不会调用您的委托方法。

【讨论】:

  • 似乎没有什么不同。同样,我正在模拟器上进行测试。将在星期一测试设备。
  • 对不起,我的错误。你完全正确。我现在收到“确实更新到位置”的回调。我现在的问题是使用 span 的缩放不起作用。对此有什么进一步的想法吗?
  • +1,为我工作。这是我的唯一问题。干杯。
【解决方案6】:

在我的情况下,它没有进入 didChangeAuthorization。我在真机上试用。

我从手机中删除了应用程序并重新安装。它有效!

希望这可以帮助某人:)

【讨论】:

    【解决方案7】:

    对于 Swift 3

    方法改为:

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])

    来自:

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]!)

    注意“_”和“位置”转换为 [CLLocation] 而不是 [AnyObject]!

    如果您使用旧方法,它将永远不会被调用,并且您不会收到它已更改的警告。

    【讨论】:

      猜你喜欢
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-17
      • 2014-11-13
      • 2013-12-20
      • 2016-05-19
      • 2016-05-24
      相关资源
      最近更新 更多