【问题标题】:showsUserLocation action attached to button not working显示附加到按钮的用户位置操作不起作用
【发布时间】:2015-01-16 03:36:18
【问题描述】:

我的应用程序中的地图视图出现问题。我创建了一个按钮,单击该按钮应在地图上显示用户位置,但没有任何反应(没有错误消息出现)。

我认为问题可能在于我编写代表的方式。相关 .h 和 .m 文件的代码如下:

ma​​pViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface mapViewController : UIViewController  {
MKMapView *mapview;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapview;
-(IBAction)setMap:(id)sender;
-(IBAction)getCurrentLocation:(id)sender;
@property (nonatomic, retain) IBOutlet CLLocationManager *locationManager;
@end

ma​​pViewController.m

#import "mapViewController.h"
@interface mapViewController ()
@end
@implementation mapViewController {
CLLocationManager *locationManager;
}
@synthesize mapview;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.requestWhenInUseAuthorization;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)setMap:(id)sender {
switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
case 0:
    mapview.mapType = MKMapTypeStandard;
    break;
case 1:
    mapview.mapType = MKMapTypeSatellite;
    break;
case 2:
    mapview.mapType = MKMapTypeHybrid;
    break;
   default:
    break;
}
}
-(IBAction)getCurrentLocation:(id)sender {
mapview.showsUserLocation = YES;
}
@end

任何帮助将不胜感激,谢谢

【问题讨论】:

  • 您期望发生什么?操作如何与 Interface Builder 中的按钮相关联?
  • 正如我上面提到的,当点击按钮时,它应该在地图上显示用户的位置。我有一个已发送的事件(内部修饰)连接到指向 getCurrentLocation 的按钮
  • 您的问题仍然不清楚,但是从文档中可以看出:“此属性并不表示用户的位置是否在地图上实际可见,仅表示地图视图是否应尝试显示它。” .除了将该属性设置为 YES 之外,您还必须设置中心坐标和(可能)缩放以获得我推断您正在寻找的行为。

标签: ios8 mapkit location-services


【解决方案1】:

您必须实现 MapKit 委托。 (确保在视图控制器的.h 中添加委托签名)

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

额外:处理App是否在前台:

我们添加 2 个事件观察者来观察 App 正在进入后台/返回活动状态:

- (void)viewDidLoad{
  [super viewDidLoad];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appToBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appReturnsActive) name:UIApplicationDidBecomeActiveNotification object:nil];
} 

- (void)appToBackground{
  [mapview setShowsUserLocation:NO];
}

- (void)appReturnsActive{
  [mapview setShowsUserLocation:YES];
}

【讨论】:

    猜你喜欢
    • 2016-03-03
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 2019-02-01
    • 2017-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多