简体   繁体   中英

mapview autozoom on current location automatic

I have one problem. I've been looking at the other answers here on stack overflow about the same question and I can't get them to work for me. So I'm going to try asking here. What I want to do is when I'm on map view and have got the user location I want to automatically zoom into the location.

the h-file

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface WalkingTableViewController: UIViewController <MKMapViewDelegate>




@property (weak, nonatomic) IBOutlet MKMapView *MKMapView;







@end

the m-file

#import "WalkingTableViewController.h"


@interface UITableViewController ()

@end

@implementation WalkingTableViewController

- (void)viewDidLoad

{
    [super viewDidLoad];
    self.MKMapView.showsUserLocation=YES;
    self.MKMapView.delegate = self;
    [self.MKMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];



}



- (IBAction)StopButton:(id)sender 

- (IBAction)StartButton:(id)sender 



@end

If you like to see the zoom animated, I would place the code in the viewDidAppear method, so that the animation starts once the view controller is displayed.

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CLLocationManager* tempLocationManager = [[CLLocationManager alloc] init];
    MKCoordinateRegion tRegion = MKCoordinateRegionMakeWithDistance([tempLocationManager.location coordinate], 20000, 20000);
    [self.MKMapView setRegion:tRegion animated:animated];
}

Adjust the region (zoom level, 20000 in the example above) to your needs.

What you have to do is tell the mapview to zoom into a region. This should get you started:

CLLocationCoordinate2D center = _mapView.userLocation.coordinate;
MKCoordinateRegion region = MKCoordinateRegionMake(center, MKCoordinateSpanMake(2000, 2000));
[_mapView setRegion:region animated:YES];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM