所以现在我至少得到了以下代码的回调......
- (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的缩放不起作用。知道为什么吗?它有我的坐标和一切。在这一个上刮我的头。
答案 0 :(得分:15)
将CLLocationManager分配给类上的(强)属性。 (我假设你正在使用ARC BTW。)现在CLLocationManager没有超过viewDidLoad方法的结尾,所以它也不会调用你的委托方法。
答案 1 :(得分:14)
确保您在<CLLocationManagerDelegate>
文件中添加了@interface
。
修改强>:
如果代理设置正确,请确保您使用的是locationManager
媒体资源:
在.h
文件中:
@property (nonatomic, strong) CLLocationManager *locationManager;
在viewDidLoad
:
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager startUpdatingLocation];
答案 2 :(得分:5)
我认为,你可以通过两种方式开展这项工作:
检查一下,你已经采用了带有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
答案 3 :(得分:1)
首先,您可以从不确保位置管理员能够首先更新位置。更新期间可能存在错误,或者您无权访问用户的位置。
实现此CLLocationManager委托方法并验证错误。
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
“此方法的实现是可选的。但是应该实现此方法。”
答案 4 :(得分:1)
如果您在模拟器中运行它,则可能需要提示它更改坐标。在Xcode中,调试输出窗格上方有一个条,带有典型的位置服务箭头。旁边是一个位置下拉列表。应用程序运行后,切换它正在模拟的位置,看看该更改是否会触发您的代码。然后在真实设备上测试它。
答案 5 :(得分:0)
就我而言,它没有进入didChangeAuthorization。我正在试用一台真正的设备。
我从手机中删除了该应用程序并重新安装。它有效!
希望这有助于某人:)
答案 6 :(得分:0)
适用于Swift 3
该方法已更改为:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
从:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]!)
注意&#39; _&#39;以及&#39;地点&#39;到[CLLocation]而不是[AnyObject]!
如果你使用旧方法,它将永远不会被调用,你不会收到警告它已经改变。