Ios8 显示附加到按钮的SUSERLOCATION操作不工作
我的应用程序中的地图视图有问题。我已经创建了一个按钮,当点击它时,它会显示用户在地图上的位置,但不会发生任何事情(不会出现错误消息) 我相信问题可能在于我给代表们写的方式。相关.h和.m文件中的代码如下: mapViewController.hIos8 显示附加到按钮的SUSERLOCATION操作不工作,ios8,mapkit,location-services,Ios8,Mapkit,Location Services,我的应用程序中的地图视图有问题。我已经创建了一个按钮,当点击它时,它会显示用户在地图上的位置,但不会发生任何事情(不会出现错误消息) 我相信问题可能在于我给代表们写的方式。相关.h和.m文件中的代码如下: mapViewController.h #import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> @interface mapViewCon
#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
如果您需要帮助,我们将不胜感激,谢谢您必须实施MapKit学员。(确保在视图控制器的
.h
中添加代理签名)
额外:处理应用程序是否在前台: 我们添加了2个事件观察员,以观察应用程序进入后台/返回活动状态:
- (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];
}
你期望发生什么?操作是如何连接到Interface Builder中的按钮的?正如我前面提到的,当单击按钮时,它应该在地图上显示用户的位置。我有一个已发送事件(内部触碰)连接到按钮,该按钮指向GetCurrentLocation您的问题仍然不清楚,但是从文档中可以看出:“此属性不指示用户的位置在地图上是否实际可见,仅指示地图视图是否应尝试显示它。”。除了将该属性设置为“是”之外,还必须设置中心坐标和(可能)缩放,以获得我推断您正在寻找的行为。
- (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];
}
- (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];
}