簡體   English   中英

如何將MKMapView縮放到用戶的當前位置

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

我試圖將mkmapview顯示給用戶當前位置。 他們將僅顯示城市名稱。 但是他們沒有指定確切的用戶當前位置。 我想顯示用戶的確切位置。 請讓我知道如何縮放用戶當前位置。

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

獲取用戶位置的另一個選項是在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];
}

步驟: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)在您的viewController.h中聲明MKCoordinateRegion

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

您可以設置latitudeDeltalongitudeDelta值來自定義縮放細節。 增大表示縮放,減小表示縮小。

viewDidLoad:首先顯示showsUserLocation viewDidLoad:像下面這樣。

mapView.showsUserLocation = YES;

然后在didUpdateToLocation:將具有跨度的Region設置為新的位置,例如波紋管...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM