Col*_*ane 8 iphone zoom mkmapview cllocationmanager ios
所以现在我至少得到以下代码的回调...
- (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];
}
Run Code Online (Sandbox Code Playgroud)
我可以在第二种方法中设置断点,NSLog报告连续的位置更新,但由于某种原因,带有span的缩放不起作用.知道为什么吗?它有我的坐标和一切.在这一个上刮我的头.
Joh*_*ool 15
将CLLocationManager分配给类上的(强)属性.(我假设你正在使用ARC BTW.)现在CLLocationManager没有超过viewDidLoad方法的结尾,所以它也不会调用你的委托方法.
nev*_*ing 14
确保您已添加<CLLocationManagerDelegate>到@interface文件中.
编辑:
如果代理设置正确,请确保您使用的是您的locationManager财产:
在.h文件中:
@property (nonatomic, strong) CLLocationManager *locationManager;
Run Code Online (Sandbox Code Playgroud)
在viewDidLoad:
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager startUpdatingLocation];
Run Code Online (Sandbox Code Playgroud)
我想,你可以通过两种方式完成这项工作:
检查一下,您已经采用了带有CLLocationManagerDelegate方法的ViEWController
#import<MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate,
MKMapViewDelegate>
{
CLLocationCoordinate2D location;
MKMapView *mapView;
}
@end
Run Code Online (Sandbox Code Playgroud)
在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
Run Code Online (Sandbox Code Playgroud)
2.使用相同的MKMapKit框架您可以使用名为didUpdateUserLocation的MKMapViewDelegate方法来执行此操作:此处您不需要CLLocaionManager,这将通过以下方式完成:在ViewController.h中:
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController < MKMapViewDelegate>
{
CLLocationCoordinate2D location;
MKMapView *mapView;
}
@end
Run Code Online (Sandbox Code Playgroud)
和在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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18553 次 |
| 最近记录: |