MKMapView 位置不会更新



在Apple的iOS Devs论坛上发布没有答案后,我试图看看是否有人对此类问题有任何经验。我正在使用 iOS 7 的 xcode。

地图视图不会更新。我很确定问题出在模拟器中,尽管我不确定如何检查它。我很想在真实设备上进行这个项目,但我的 iOS 7 设备仍在商店中。

我创建了一个嵌套在视图下的MKMapView(嵌套在ViewController下)->将CoreLocation和MapKit添加到框架中->将MKMapView连接到右侧属性mapView。

我的文件如下所示:

MapScanViewController.h:

#import <UIKit/UIKit.h>    
#import <MapKit/MapKit.h>
@interface MapScanViewController : UIViewController<MKMapViewDelegate>
// I was trying to create a label and print the 
// userLocation description into the label - but none worked
@property (nonatomic,retain) IBOutlet UILabel *myLabel;
@property (nonatomic,strong) IBOutlet MKMapView *mapView;
@end

MapScanViewController.m

#import "MapScanViewController.h"
@implementation MapScanViewController
- (void)viewDidLoad    
{
    [super viewDidLoad];
    _mapView.delegate = self;
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
    [_mapView setRegion:[_mapView regionThatFits:region] animated:YES];
    // Label idea did not work eiter
    [_myLabel setText:[userLocation description]];    
}
@end

当我尝试将位置更改为Apple的无限循环时,地图保持不变,就像什么都没有改变一样。还尝试骑自行车和跑步,但什么也没发生。我尝试重置iOS模拟器几次,但根本没有帮助,当然也尝试从xcode中手动设置位置。

尝试添加/强制此操作(也无济于事):

if (_mapView.showsUserLocation)
{
_mapView.showsUserLocation = NO;
_mapView.showsUserLocation = YES;
}

没有一个对我有用。MapView只是不会回应。


我验证了地图视图

// Meters-Miles convertion
#define METERS_PER_MILE 1609.344
// Just wanted to check if MapView's dead or not - worked- it took me to see (39.28..., -76.58...)
- (void)viewWillAppear:(BOOL)animated {
    // 1
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 39.281516;
    zoomLocation.longitude= -76.580806;
    // 2
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
    // 3
    [_mapView setRegion:viewRegion animated:YES];
}

从地图上获取任何内容之前,您必须调用_mapView.showsUserLocation = YES;

该调用将提示用户访问位置服务。

正如 Daij-Djan 所说 尝试这样

- (void)viewDidLoad    
{
 _mapView.showsUserLocation = YES;
}

最新更新