在 info plist 中写下这一行
CLLocationManager requestAlwaysAuthorization (string) abcd
//代码并设置注释引脚
。H
#import<MapKit/Mapkit.h>
CLLocationManager *locationManager;
@property(nonatomic, weak) IBOutlet MKMapView* mapView;
@property(nonatomic,retain)CLLocationManager* locationManager;
- (id)init {
self = [super init];
if(self != nil) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
}
return self;
}
在 viewDidLoad
_mapView.showsUserLocation = YES;
_mapView.mapType = MKMapTypeStandard;
_mapView.delegate = self;
if(IS_OS_8_OR_LATER) {
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
// NSDictionary *dictlatitude = [_dict objectForKey:@"latitude"]; float strlatitude = [[_dict objectForKey:@"latitude"] floatValue];
// NSDictionary *dictlongitude = [_dict objectForKey:@"longitude"]; float strlongitude = [[_dict objectForKey:@"longitude"] floatValue];
NSLog(@"Staring Point latitude : %f", strlatitude);
NSLog(@"Staring Point longitude: %f", strlongitude);
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;
span.longitudeDelta = 0.005;
CLLocationCoordinate2D location;
location.latitude = strlatitude;
location.longitude = strlongitude;
region.span = span;
region.center = location;
[_mapView setRegion:region animated:YES];
MyAnnotation *ann = [[MyAnnotation alloc] init];
ann.title=@"name of the pin";
ann.coordinate = region.center;
[_mapView addAnnotation:ann];