Objective-C MKMapView 定位到用户位置的中心

32
我想把用户位置作为屏幕的中心参考点进行缩放。我有以下代码:
MainViewController.h
#import <UIKit/UIKit.h>
#import "FlipsideViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

IBOutlet MKMapView *mapView;

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate, MKMapViewDelegate> {
   MKMapView *mapView;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;

MainViewController.m

@implementation MainViewController
@synthesize mapView;

 - (void)viewDidLoad {
    [super viewDidLoad];
    mapView = [[MKMapView alloc]
           initWithFrame:self.view.bounds
           ];
    mapView.showsUserLocation = YES;
    mapView.mapType = MKMapTypeHybrid;
    mapView.delegate = self;
    [self.view addSubview:mapView];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.005;
    span.longitudeDelta = 0.005;
    CLLocationCoordinate2D location;
    location.latitude = userLocation.coordinate.latitude;
    location.longitude = userLocation.coordinate.longitude;
    region.span = span;
    region.center = location;
    [mapView setRegion:region animated:YES];
 }

现在我只在最后一行 [mapView setRegion:region animated:YES] 上收到一个构建警告,它表明:“'mapView' 的局部声明隐藏了实例变量”。

2个回答

72
当你执行 mapView.showsUserLocation = YES; 时,你请求它获取用户位置。这并不是瞬间发生的。由于需要时间,地图视图将通过委托方法mapView:didUpdateUserLocation通知其委托方可获得用户位置。因此,你应该采用MKMapViewDelegate协议并实现该方法。你应该将所有缩放代码移动到此方法中。 设置委托
- (void)viewDidLoad {
    [super viewDidLoad];
    mapView = [[MKMapView alloc]
           initWithFrame:CGRectMake(0, 
                                    0,
                                    self.view.bounds.size.width, 
                                    self.view.bounds.size.height)
           ];
    mapView.showsUserLocation = YES;
    mapView.mapType = MKMapTypeHybrid;
    mapView.delegate = self;
    [self.view addSubview:mapView];
}

更新的委托方法

- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation {
    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.005;
    span.longitudeDelta = 0.005;
    CLLocationCoordinate2D location;
    location.latitude = aUserLocation.coordinate.latitude;
    location.longitude = aUserLocation.coordinate.longitude;
    region.span = span;
    region.center = location;
    [aMapView setRegion:region animated:YES];
}

1
我对Objective-C非常陌生,请耐心等待...我该如何在didViewLoad方法中调用didUpdateUserLocation方法? - sadmicrowave
@Deepak - 就像我所说的(请检查我的 OP 以获取删除部分的更新),这一行会出现错误:“MainViewController 没有实现 MKMapViewDelegate 协议”。 - sadmicrowave
2
你按照 Srikar 的说法做了这个 @interface MainViewController : UIViewController <FlipsideViewControllerDelegate, MKMapViewDelegate> 吗? - Deepak Danduprolu
2
还有一件事,委托方法是 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 而不是方法简写 mapView:didUpdateUserLocation: - Deepak Danduprolu
2
本地变量掩盖了实例变量。调整了委托方法并将其编辑到我的答案中。 - Deepak Danduprolu
显示剩余9条评论

3
在你的接口中,你忘记继承MapViewDelegate。
#import <MapKit/MapKit.h>

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate, MKMapViewDelegate> 
{
   MKMapView *mapView;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;

休息看起来不错。


我添加了您的@property行,但仍然收到相同的问题错误。 - sadmicrowave
我改了一些东西在我的原始代码上,请重新检查。 - sadmicrowave

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接