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

我的代码是这样的:

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

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

我也已经在真实设备中对其进行了测试,但是它没有显示我的位置。

加载时, map 看起来像这样:

ios - 本地图载入IOS时,以动画放大当前位置-LMLPHP

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

最佳答案

将这些行添加到viewDidLoad中:

self.mapView.showsUserLocation = YES;

如果要将 map 居中放置在用户位置,请添加以下内容:
[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键。

如果您在模拟器上更改位置,则可以看到 map 更改位置。

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

新编辑:

您的代码中有错误!

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

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

关于ios - 本地图载入IOS时,以动画放大当前位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47326124/

10-09 15:34
查看更多