缩放到用户位置iOS 8 MapKit Obj-C

时间:2015-02-27 04:50:32

标签: objective-c ios8 mapkit

我没有显示用户当前位置的问题,但我在尝试缩放时遇到问题。我在这里找到了其他帖子的一些解决方案,但似乎没有任何效果。建议很受赞赏,因为我很可能错过了一些简单的东西。

这是我到目前为止所做的:

@import CoreLocation;

@interface ViewController () <CLLocationManagerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Get authorization
    CLLocationManager * locationManager = [[CLLocationManager alloc] init];
    // Check before requesting, otherwise it might crash older versions of ios
    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    [locationManager requestWhenInUseAuthorization];
}

self.mapView.showsUserLocation = YES;
self.mapView = [[MKMapView alloc] init];
self.mapView.delegate = self;
self.mapView.scrollEnabled = YES;
self.mapView.zoomEnabled = YES;
self.mapView.userTrackingMode = YES;

}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation     *)userLocation
{
//CLLocationCoordinate2D loc = [userLocation coordinate];
//MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 1000, 1000);
//[self.mapView setRegion:region animated:YES];


//Zoom map to users current location
MKCoordinateRegion region;
MKCoordinateSpan span;

span.latitudeDelta = 0.00001;
span.longitudeDelta = 0.00001;

CLLocationCoordinate2D location = mapView.userLocation.coordinate;

region.span = span;
region.center = location;

[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];

}

@end

1 个答案:

答案 0 :(得分:1)

我做了类似的事

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
        if (!self.initialLocation) {
            self.initialLocation = userLocation.location;
            MKCoordinateRegion mapRegion;
            mapRegion.center = mapView.userLocation.coordinate;
            mapRegion.span.latitudeDelta = 0.2;
            mapRegion.span.longitudeDelta = 0.2;

            [mapView setRegion:mapRegion animated: YES];
        }

    }

self.initialLocationCLLocation的一个实例。这种方法不断更新用户的当前位置,所以我要做的就是检查它是否为nil,它应该是开始的,然后运行代码放大。第一次之后,{{ 1}}永远不会是零。

如果你不断想要放大用户的当前位置......摆脱self.initialLocation声明。

修改

在.h文件中为self.initialLocation

设置此项

if

编辑2

删除此行

@property (strong, nonatomic) CLLocation *initialLocation;

无需重新初始化您的IBOulet