xcode 如何将 MKMapView 缩放到用户的当前位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17547171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:35:19  来源:igfitidea点击:

How do I zoom an MKMapView to the user's current location

iosobjective-cxcodemkmapview

提问by user2563884

I am trying to show the mkmapview to the users current location. They will display only city name. But they are not specify the exact users current location. I want to display the users exact location. Please give me any idea how to zoom the users current location.

我正在尝试向用户当前位置显示 mkmapview。它们将仅显示城市名称。但他们没有指定用户当前的确切位置。我想显示用户的确切位置。请告诉我如何缩放用户当前位置。

GPSlocationAPPDelegate.m

GPSlocationAPPDelegate.m

- (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;      
}

回答by Bhavesh Nayi

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];

CLLocation *location = [locationManager location];

coordinate = [location coordinate];

latitude = coordinate.latitude;
longitude = coordinate.longitude;
mapview.region = MKCoordinateRegionMakeWithDistance(coordinate, 10000, 10000);

回答by Shridhar Mali

Steps: 1) In ViewWillAppear

步骤:1)在ViewWillAppear

[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) Declare MKCoordinateRegion In your viewController.h

2) 在 vi​​ewController.h 中声明 MKCoordinateRegion

MKCoordinateRegion region;

MKCoordinateRegion 区域;

3) In didUpdateToLocation:

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];
}

回答by SachinVsSachin

Other option to get the user location is adding observer to mapview in viewDidLoad method like this

获取用户位置的其他选项是在像这样的 viewDidLoad 方法中向 mapview 添加观察者

-(void)viewDidLoad
{
    [super viewDidLoad]
    //register the observer
    [self.mapView.userLocation addObserver:self  
                                forKeyPath:@"location"  
                                   options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)  
                                   context:NULL];
}

and whenever this observer will call just do this

每当这个观察者打电话时,就这样做

- (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];
}

回答by karthika

self.mapView.showsUserLocation = YES;  
MKCoordinateRegion region;
region.center = self.mapView.userLocation.coordinate;
MKCoordinateSpan span;
span.latitudeDelta  = 20; 
span.longitudeDelta = 20;
region.span = span;
[self.realtorMapView setRegion:region animated:YES];

回答by Bhavin_m

You can set latitudeDeltaand longitudeDeltavalue to customised your zooming details. Increase means zoom and decrease means zoom out.

您可以设置latitudeDeltalongitudeDelta值来自定义您的缩放细节。增大表示放大,减小表示缩小。

回答by Paras Joshi

First in set showsUserLocationin viewDidLoad:like bellow..

首先在如下设置showsUserLocationviewDidLoad:..

mapView.showsUserLocation = YES;

and then in didUpdateToLocation:set Region with span with new location like bellow...

然后在didUpdateToLocation:具有新位置的跨度设置区域中,如下所示...

-(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...
}