Ios 如何将MKMapView缩放到用户';s当前位置
我正在尝试向用户当前位置显示mkmapview。它们将只显示城市名称。但它们并没有指定用户当前的确切位置。我想显示用户的确切位置。请告诉我如何缩放用户当前位置 GPSlocationAPPDelegate.mIos 如何将MKMapView缩放到用户';s当前位置,ios,objective-c,xcode,mkmapview,Ios,Objective C,Xcode,Mkmapview,我正在尝试向用户当前位置显示mkmapview。它们将只显示城市名称。但它们并没有指定用户当前的确切位置。我想显示用户的确切位置。请告诉我如何缩放用户当前位置 GPSlocationAPPDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.locationManager=[[CLLocationManager alloc]init];
if([CLLocationManager locationServicesEnabled])
{
self.locationManager.delegate=self;
self.locationManager.distanceFilter=1;
[self.locationManager startUpdatingLocation];
}
self.viewController = [[AnimationViewController alloc] initWithNibName:@"AnimationViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
double miles=12.0;
double scalingFactor= ABS( cos(2 * M_PI * newLocation.coordinate.latitude /360.0) );
MKCoordinateSpan span;
span.latitudeDelta=miles/69.0;
span.longitudeDelta=miles/(scalingFactor*69.0);
MKCoordinateRegion region;
region.span=span;
region.center=newLocation.coordinate;
[self.viewController.mapView setRegion:region animated:YES];
self.viewController.mapView.showsUserLocation=YES;
}
获取用户位置的另一个选项是在viewDidLoad方法中将观察者添加到mapview,如下所示
-(void)viewDidLoad
{
[super viewDidLoad]
//register the observer
[self.mapView.userLocation addObserver:self
forKeyPath:@"location"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context:NULL];
}
无论这个观察家什么时候打电话,就这么做
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
MKCoordinateRegion region;
region.center = self.mapView.userLocation.coordinate;
//Adjust span as you like
MKCoordinateSpan span;
span.latitudeDelta = 1;
span.longitudeDelta = 1;
region.span = span;
[self.mapView setRegion:region animated:YES];
}
获取用户位置的另一个选项是在viewDidLoad方法中将观察者添加到mapview,如下所示
-(void)viewDidLoad
{
[super viewDidLoad]
//register the observer
[self.mapView.userLocation addObserver:self
forKeyPath:@"location"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context:NULL];
}
无论这个观察家什么时候打电话,就这么做
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
MKCoordinateRegion region;
region.center = self.mapView.userLocation.coordinate;
//Adjust span as you like
MKCoordinateSpan span;
span.latitudeDelta = 1;
span.longitudeDelta = 1;
region.span = span;
[self.mapView setRegion:region animated:YES];
}
您可以将
latitudeDelta
和longitudeDelta
值设置为自定义缩放细节。增大表示缩放,减小表示缩小。您可以设置latitudeDelta
和longitudeDelta
值以自定义缩放细节。增加表示缩放,减少表示缩小。集合中的第一个显示位置视图中的加载:
如下图所示
mapView.showsUserLocation = YES;
然后在didUpdateToLocation:
中,用span设置区域,新位置如下所示
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
MKCoordinateRegion region;
region.center = newLocation.coordinate;
MKCoordinateSpan span;
span.latitudeDelta=0.04;
span.longitudeDelta=0.04;
region.span=span;
region.center = currentLocation;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region]; // Set `regionThatFits:` with `region` like bellow...
}
集合中的第一个在视图中显示位置加载:
如下图所示
mapView.showsUserLocation = YES;
然后在didUpdateToLocation:
中,用span设置区域,新位置如下所示
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
MKCoordinateRegion region;
region.center = newLocation.coordinate;
MKCoordinateSpan span;
span.latitudeDelta=0.04;
span.longitudeDelta=0.04;
region.span=span;
region.center = currentLocation;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region]; // Set `regionThatFits:` with `region` like bellow...
}
步骤:
1) 在视图中将出现
[self.mapView setMapType:MKMapTypeStandard];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];
[self.mapView setDelegate:self];
locationManager = [[CLLocationManager alloc] init] ;
BOOL locationAllowed = [CLLocationManager locationServicesEnabled];
if (locationAllowed == NO){
//GPS DISABLED.
}
else{
if(IS_IOS8_OR_LATER){
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
}
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
2) 在viewController.h中声明MKCoordinateRegion
协调区域
3) 在didUpdateToLocation中:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"current Latitude is %f",newLocation.coordinate.latitude);
NSLog(@"current Longitude is %f",newLocation.coordinate.longitude);
region.span.longitudeDelta *= 0.05;
region.span.latitudeDelta *= 0.05;
region.center.latitude = newLocation.coordinate.latitude;
region.center.longitude = newLocation.coordinate.longitude;
[self.mapView setRegion:region animated:YES];
[locationManager stopUpdatingLocation];
}
步骤:
1) 在视图中将出现
[self.mapView setMapType:MKMapTypeStandard];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];
[self.mapView setDelegate:self];
locationManager = [[CLLocationManager alloc] init] ;
BOOL locationAllowed = [CLLocationManager locationServicesEnabled];
if (locationAllowed == NO){
//GPS DISABLED.
}
else{
if(IS_IOS8_OR_LATER){
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
}
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
2) 在viewController.h中声明MKCoordinateRegion
协调区域
3) 在didUpdateToLocation中:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"current Latitude is %f",newLocation.coordinate.latitude);
NSLog(@"current Longitude is %f",newLocation.coordinate.longitude);
region.span.longitudeDelta *= 0.05;
region.span.latitudeDelta *= 0.05;
region.center.latitude = newLocation.coordinate.latitude;
region.center.longitude = newLocation.coordinate.longitude;
[self.mapView setRegion:region animated:YES];
[locationManager stopUpdatingLocation];
}
即使mapView的用户位置处于关闭状态,也可以进行更正和工作。即使mapView的用户位置处于关闭状态,也可以进行更正和工作。改进:您需要使用CLLocationManagerDelegate
并侦听位置中的更改。在那里,您可以使用newLocation
并更新到视图。改进:您需要使用CLLocationManagerDelegate
并收听位置的更改。在那里,您可以使用newLocation
并更新视图。