ios Objective-c MKMapView 以用户位置为中心
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6169919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
objective-c MKMapView center on user location
提问by sadmicrowave
I'm trying to zoom into the user location as the center reference for the screen. I have this code:
我试图放大用户位置作为屏幕的中心参考。我有这个代码:
MainViewController.h
主视图控制器.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
主视图控制器.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];
}
Now I'm only getting a build warning on the last line [mapView setRegion:region animated:YES]
stating: 'local declaration of 'mapView' hides instance variable'
现在我只在最后一行收到一个构建警告,[mapView setRegion:region animated:YES]
说明:''mapView' 的本地声明隐藏了实例变量'
回答by Deepak Danduprolu
When you do mapView.showsUserLocation = YES;
, you ask it to retrieve the user location. This doesn't happen instantly. As it takes time, the map view notifies its delegate that a user location is available via the delegate method mapView:didUpdateUserLocation
. So you should adopt the MKMapViewDelegate
protocol and implement that method. You should move all your zooming-in code to this method.
当你这样做时mapView.showsUserLocation = YES;
,你要求它检索用户位置。这不会立即发生。由于需要时间,地图视图通过委托方法通知其委托用户位置可用mapView:didUpdateUserLocation
。因此,您应该采用该MKMapViewDelegate
协议并实施该方法。您应该将所有放大代码移至此方法。
Setting the delegate
设置委托
- (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];
}
Updated delegate method
更新的委托方法
- (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];
}
回答by Srikar Appalaraju
In your interface you forgot to inherit MapViewDelegate
-
在您的界面中,您忘记继承MapViewDelegate
-
#import <MapKit/MapKit.h>
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate, MKMapViewDelegate>
{
MKMapView *mapView;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
Rest seems fine.
休息似乎很好。