【问题标题】:Get location about didUpdateLocations and didUpdateToLocation never call on ios 5.1 and ios6获取有关 didUpdateLocations 和 didUpdateToLocation 的位置从不调用 ios 5.1 和 ios6
【发布时间】:2013-10-23 09:14:07
【问题描述】:

我有一个关于位置的问题。

我需要在 iOS 5.1 和 iOS 6 上获取经纬度位置。

但是我在 didUpdateLocations 和 didUpdateToLocation 函数中编写了 NSLog。

我没有在控制台看到日志显示。

我正在使用模拟设置位置。我测试的纬度值为25.317973,经度值为121.538161(D1)。

那么我的测试值为 (25.917973, 121.538161) (D2)。

D1和D2距离大于10米;

但我从未在控制台中看到日志显示。

我在下面附上我的代码:

我的 .h 文件

#mport <GoogleMaps/GoogleMaps.h>
#import <CoreLocation/CoreLocation.h>
#import <Corelocation/CLLocationManagerDelegate.h> 

@interface LocationMapViewController : AbstractViewController<     CLLocationManagerDelegate, GMSMapViewDelegate >
@property (strong, nonatomic) CLLocationManager *locationManager;
@end

我的 .m 文件

   - (void)viewDidLoad
       {
           [super viewDidLoad];

 if( _locationManager == nil )
    {
        _locationManager =  [CLLocationManager new];
        _locationManager.delegate = self;

        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.distanceFilter = 10.0f;
        [_locationManager startUpdatingHeading];

    }

-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray     *)locations
{
    NSLog(@"==latitude %f, longitude %f", [[locations objectAtIndex:([locations     count]-1)] coordinate].latitude , [[locations objectAtIndex:([locations count]-1)]     coordinate].longitude);
}

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation     *)newLocation fromLocation:(CLLocation *)oldLocation
{
     NSLog(@"====latitude %f, longitude %f", newLocation.coordinate.latitude,     newLocation.coordinate.longitude);
}

有人知道我的代码有什么问题吗?

非常感谢

【问题讨论】:

    标签: ios google-maps location


    【解决方案1】:

    您不必在 .h 和 AFAIK 中导入 CLLocationManagerDelegate,我认为 CoreLocation 也没有使用 Google 地图,所以除非您使用 Google Maps API 或其他东西,否则不需要导入谷歌地图也是如此。

    尝试在您的方法中使用它:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        if( _locationManager == nil )
        {
           _locationManager = [[CLLocationManager alloc] init];
           _locationManager.delegate = self;
           _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
           [_locationManager startUpdatingLocation];
        }
    }
    

    编辑:

    我意识到您正在尝试获取距离的差异。所以也许只是改变你的

    _locationManager = [CLLocationManager new];
    

    _locationManager = [[CLLocationManager alloc] init];
    

    这应该可以解决问题。

    根据您的评论,我检查了更多,并意识到您正在使用startUpdatingHeading 方法调用,因此您应该使用didUpdateHeading 委托方法。这是来自 Apple CLLocationManager 参考:

    标题事件被传递到你的委托的 locationManager:didUpdateHeading: 方法。如果出现错误,位置管理器将调用您的委托的 locationManager:didFailWithError: 方法。

    希望这会有所帮助! :)

    【讨论】:

    • 我试过你的方法。但是我的控制台中仍然没有任何日志。 :(我试过设置距离差。我不知道是什么问题。我用过google maps api,所以我导入了这个头文件。谢谢
    • 我明白我的错误在哪里。因为我没有将 startUpdatingHeading 改为 startUpdatingLocation。非常感谢你,命运猪。
    猜你喜欢
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 2013-07-22
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    相关资源
    最近更新 更多