【问题标题】:objective-c MKMapView center on user locationObjective-c MKMapView 以用户位置为中心
【发布时间】:2011-09-04 10:09:55
【问题描述】:

我正在尝试放大用户位置作为屏幕的中心参考。我有这个代码:

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' 的本地声明隐藏实例变量'

【问题讨论】:

    标签: ios objective-c animation gps mkmapview


    【解决方案1】:

    当您执行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];
    }
    

    【讨论】:

    • 我对 Objective-c 非常陌生,所以请耐心等待...如何从 didViewLoad 方法中调用 didUpdateUserLocation 方法?
    • 你没有。正如@Srikar 指出的那样,您采用了该协议,并且您似乎已经这样做了,然后将自己指定为它的代表,例如mapView.delegate = self;。地图视图会在有必要信息时自行通知您。
    • 你有没有像 Srikar 说的那样 @interface MainViewController : UIViewController &lt;FlipsideViewControllerDelegate, MKMapViewDelegate&gt; 做到这一点?
    • 最后一件事是委托方法是- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation,而不是方法简写mapView:didUpdateUserLocation:
    • 局部变量掩盖了实例变量。调整了委托方法并将其编辑到我的答案中。
    【解决方案2】:

    在你的界面中你忘了继承MapViewDelegate -

    #import <MapKit/MapKit.h>
    
    @interface MainViewController : UIViewController <FlipsideViewControllerDelegate, MKMapViewDelegate> 
    {
       MKMapView *mapView;
    }
    @property (nonatomic, retain) IBOutlet MKMapView *mapView;
    

    休息似乎很好。

    【讨论】:

    • 我添加了您的@property 行,但仍然收到相同的问题错误
    • 我在我的 OP 中更改了一些内容,请再次检查
    猜你喜欢
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    相关资源
    最近更新 更多