出于某种原因,当按钮" GetLocation"时,我的地图不会显示用户位置。被压了。我也确定了#34; GetLocation"被委托给viewcontroller上的按钮。我已将此代码与此论坛中的其他人进行了比较,并且已经坚持了很长一段时间......
// ViewController .h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKFoundation.h>
#import <MapKit/MKAnnotationView.h>
#import <CoreLocation/CoreLocation.h>
@interface testmap : UIViewController <MKAnnotation, MKMapViewDelegate, CLLocationManagerDelegate> {
IBOutlet MKMapView *mapView;
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *name;
@property(nonatomic, retain) IBOutlet MKMapView *mapView;
@property (strong) CLLocationManager *locationManager;
- (id)initWithLocation:(CLLocationCoordinate2D)coord;
- (IBAction)GetLocation:(id)sender;
@end
// ViewController .m
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#import "testmap.h"
- (NSString *)deviceLocation {
return [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
}
- (IBAction)GetLocation:(id)sender; {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
if (IS_OS_8_OR_LATER) {
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
}
mapView.showsUserLocation = YES;
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
self.mapView.showsUserLocation = YES;
self.mapView.showsPointsOfInterest = YES;
}
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}