
[英]CLLocationManager startUpdatingLocation using a UIButton
[英]CLLocationManager startUpdatingLocation not working
所以现在我至少得到以下代码的回调...
- (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的缩放不起作用。 知道为什么吗? 它有我的坐标和一切。 在这一个上刮我的头。
将CLLocationManager分配给类上的(强)属性。 (我假设你正在使用ARC BTW。)现在CLLocationManager没有超过viewDidLoad方法的结尾,所以它也不会调用你的委托方法。
确保在@interface
文件中添加了<CLLocationManagerDelegate>
。
编辑 :
如果委托设置正确,请确保您使用的是locationManager
属性:
在.h
文件中:
@property (nonatomic, strong) CLLocationManager *locationManager;
在viewDidLoad
:
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager startUpdatingLocation];
我想,你可以通过两种方式完成这项工作:
检查一下,您已经采用了带有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
嗯,首先,您永远无法确定位置管理员是否能够首先更新位置。 更新期间可能存在错误,或者您无权访问用户的位置。
实现此CLLocationManager委托方法并验证错误。
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
“此方法的实现是可选的。但是,您应该实现此方法。”
如果您在模拟器中运行它,则可能需要提示它更改坐标。 在Xcode中,调试输出窗格上方有一个条,带有典型的位置服务箭头。 旁边是一个位置下拉列表。 应用程序运行后,切换它正在模拟的位置,看看该更改是否会触发您的代码。 然后在真实设备上测试它。
就我而言,它没有进入didChangeAuthorization。 我正在尝试使用真正的设备。
我从手机中删除了该应用程序并重新安装。 它的工作原理!
希望这有助于某人:)
对于Swift 3
该方法已更改为:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
从:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]!)
注意'_'和'位置'的转换为[CLLocation]而不是[AnyObject]!
如果您使用旧方法,它将永远不会被调用,您将不会收到它已更改的警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.