在IOS中加载地图时,在当前位置放大动画

时间:2017-11-16 09:37:28

标签: ios objective-c mkmapview

我在视图控制器中使用了地图,但是当地图加载时,它显示了整个国家的视图。我想在加载地图时显示我当前的位置视图,因为我使用了didUpdateUserLocation方法并为其设置了委托。

我的代码是:

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
   [self.mapView setRegion:MKCoordinateRegionMake(_userLocation.coordinate, MKCoordinateSpanMake(0.1f, 0.1f)) animated:YES];
}

现在,当我的地图加载动画和缩放但位置错误时。当地图加载时,它会带我到海中的任何位置,但我当前的位置是不同的。

我也在真实设备中测试了它,但它没有显示我的位置。

加载时地图看起来像这样:

enter image description here

ViewController类代码:

@property BOOL updateUserLocation;
@property(strong,nonatomic) CLLocation *userLocation;

@property (nonatomic, retain) MKPolyline *routeLine;

@property (nonatomic, retain) MKPolylineView *routeLineView;

@property (nonatomic,retain) NSString *latitude;

@property (nonatomic,retain) NSString *longitude;
- (void)viewDidLoad {
[super viewDidLoad];
 [[self navigationController] setNavigationBarHidden:YES animated:YES];
_AlertView.hidden=YES;
_distanceView.hidden=YES;
self.mapView.zoomEnabled=YES;

locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];

[self.mapView setShowsUserLocation:YES];
[self.mapView setDelegate:self];

}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
[self.mapView setRegion:MKCoordinateRegionMake(_userLocation.coordinate, MKCoordinateSpanMake(0.1f, 0.1f)) animated:YES];
}

3 个答案:

答案 0 :(得分:1)

在viewDidLoad中添加以下行:

self.mapView.showsUserLocation = YES;

如果您想将地图居中到用户位置,请添加以下内容:

[self.mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];

修改

ViewController示例:

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController () <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLLocation *location;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [[self mapView] setShowsUserLocation:YES];

    self.locationManager = [[CLLocationManager alloc] init];

    // we have to setup the location manager with permission in later iOS versions
    if ([[self locationManager] respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [[self locationManager] requestWhenInUseAuthorization];
    }

    [[self locationManager] setDelegate:self];
    [[self locationManager] setDesiredAccuracy:kCLLocationAccuracyBest];
    [[self locationManager] startUpdatingLocation];
}

-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    // Stop updating location, when you want to restart update use [[self locationManager] startUpdatingLocation];
    [manager stopUpdatingLocation];

    self.location = locations.lastObject;

    // zoom the map into the users current location
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance
        (self.location.coordinate, 2, 2);
    [[self mapView] setRegion:viewRegion animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

我只在故事板上添加了IBOutlet,并在plist中添加了NSLocationWhenInUseUsageDescription键。

如果更改模拟器上的位置,则可以看到地图更改位置。

如果您愿意,我可以上传完整的示例项目。

新编辑:

您的代码出错!

当您使用[self.mapView setRegion:MKCoordinateRegionMake(_userLocation.coordinate, MKCoordinateSpanMake(0.1f, 0.1f)) animated:YES]时,使用的是您的CLLocation变量的_userLocation!您必须使用userLocation,它是您从方法中获得的值。

我建议您更改CCLocation变量名称,并仅对didUpdateUserLocation方法中使用的变量使用userLocation名称。

答案 1 :(得分:0)

您可以调用此方法并放大地图

 CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = ; // your latitude value
        zoomLocation.longitude=  // your longitude value
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta=0.18; // change as per your zoom level
    span.longitudeDelta=0.18;
    region.span=span;
    region.center= zoomLocation;
    [mapview setRegion:region animated:TRUE];
    [mapview regionThatFits:region];

答案 2 :(得分:0)

使用didUpdateToLocation代替didUpdateUserLocation

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    [self.locationManager stopUpdatingLocation];

    MKCoordinateRegion region;
    region.span.latitudeDelta = 0.005;
    region.span.longitudeDelta = 0.005;
    region.center.latitude = newLocation.coordinate.latitude;
    region.center.longitude = newLocation.coordinate.longitude;
    [self.mapView setRegion:region animated:YES];
}

另见我的图书馆。

https://github.com/koogawa/KGWLocationPicker/blob/master/KGWLocationPicker/KGWLocationPickerViewController.m