2012-07-28 85 views
8

所以现在我至少得到回调用下面的代码...CLLocationManager startUpdatingLocation不工作

- (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的报告不断位置更新,但由于某些原因跨度缩放无法正常工作。任何想法为什么?它有我的坐标和一切。在我的头上划伤我的头。

+0

乌尔代码似乎确定..问题是呃其他地方 – samfisher 2012-07-28 06:32:18

+0

你在模拟器中遇到这些麻烦吗?你是否启用了位置模拟? – HeikoG 2012-07-28 07:29:30

+0

是的,模拟器的麻烦。我已启用模拟位置。也许我需要在设备上测试它。 – Coltrane 2012-07-28 08:17:28

回答

15

将CLLocationManager分配给类中的(强)属性。 (我假设你正在使用ARC BTW。)现在,CLLocationManager不会超过viewDidLoad方法的末尾,所以它也不会调用你的委托方法。

+0

似乎没有什么区别。再次,我正在模拟器上测试。将在星期一测试一个设备。 – Coltrane 2012-07-28 17:41:09

+0

对不起,我的错。你完全正确。我现在正在获取回调“已更新到位置”。我现在的问题是,使用跨度缩放无法正常工作。对此有何进一步的想法? – Coltrane 2012-07-30 21:18:43

+0

+1,为我工作。这是我遇到的唯一问题。干杯。 – viral 2014-10-06 11:51:35

1

那么,首先,你可以从不请确保位置管理器能够在第一时间更新位置。在更新过程中可能会出现错误,或者您无权访问用户的位置。

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

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

“这种方法的实现是可选的。你应该然而实现此方法。”

+1

不报告任何错误。我在iOS模拟器上尝试这个测试。这有什么关系吗? – Coltrane 2012-07-28 04:43:28

5

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

  1. 使用CLLocation框架

检查,你已经通过了的viewController与CLLocationManagerDelegate方法

#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 
+0

使用方法1,我得到回调,但缩放(跨度)不缩放到位置。我修改了我的代码。也许看一看,看看有什么不对? – Coltrane 2012-07-30 21:17:19

13

确保您在加文件。

编辑

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

.h文件:

@property (nonatomic, strong) CLLocationManager *locationManager; 

viewDidLoad

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

这只会修复警告,而不是其他的。 – 2012-07-28 14:40:02

+0

我当然有。 – Coltrane 2012-07-28 17:45:15

1

如果你正在模拟器中运行它,那么你可能需要提示它改变坐标。在Xcode中,调试输出窗格上方有一个带有典型位置服务箭头的栏。接下来是位置下拉列表。一旦你的应用程序正在运行,切换它正在模拟的位置,看看这个改变是否会触发你的代码。然后在真实的设备上测试它。

+0

这很聪明。不幸的是它不起作用,但我喜欢这个想法。我只需要在真实的设备上进行测试。 – Coltrane 2012-07-28 17:44:24

0

在我的情况下,它没有进入didChangeAuthorization。我正在尝试一个真实的设备。

我从手机中删除了应用程序并重新安装。它的工作原理!

希望这可以帮助别人:)

0

对于斯威夫特3

的方法已更改为:从

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

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

注意'_'和'位置'到[CLLocation]而不是[AnyObject]的转换!

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

相关问题