A positioning demo and a positioning demo
Preparations
- Click the project name, slide to the bottom, and click the + sign to import the MapKit. framework system library.
- If you are using iOS8 or a later version, you need to add the following content to the info. plist file. The final character can be typed as needed.
Detailed demo practices
Finally, I will provide you with the Code. The comments are quite detailed. I believe you can understand them.
# Import "ViewController. h "# import <MapKit/MapKit. h> @ interface ViewController () <MKMapViewDelegate> @ property (weak, nonatomic) IBOutlet MKMapView * mapView; @ property (nonatomic, strong) CLLocationManager * manager; @ end @ implementation ViewController // return the current user location-(IBAction) currentLocation :( id) sender {// obtain the latitude and longitude of the user CLLocationCoordinate2D coordinate = self. mapView. userLocation. location. coordinate; MKCoordi NateSpan span = MKCoordinateSpanMake (0.1, 0.1); MKCoordinateRegion region = MKCoordinateRegionMake (coordinate, span); // locate the visible area [self. mapView setRegion: region animated: YES];}-(CLLocationManager *) manager {if (! _ Manager) {// create the location manager CLLocationManager * locationManager = [[CLLocationManager alloc] init]; // the positioning accuracy. The higher the accuracy, the higher the power consumption of locationManager. desiredAccuracy = kCLLocationAccuracyBest; // The location update frequency, in the unit of meter locationManager. distanceFilter = 5; _ manager = locationManager;} return _ manager;}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // iOS8 practices if (! [CLLocationManager locationServicesEnabled]) {NSLog (@ "");} else {// obtain the currently located status, clthorizationstatus status = [CLLocationManager authorizationStatus]; // if the positioning status is not enabled if (status = kclthorizationstatusnotdetermined) {// requestWhenInUseAuthorization front-end positioning // requestAlwaysAuthorization front-end and background positioning [self. manager requestWhenInUseAuthorization]; // [self. manager requestAlwaysAuthorization];} sel F. mapView. delegate = self;/* MKMapTypeStandard = 0, // Standard Map 2D MKMapTypeSatellite, // satellite map MKMapTypeHybrid, // hybrid map * // you can specify the map type self. mapView. mapType = MKMapTypeStandard; // Display User location self. mapView. showsUserLocation = YES;} // called when the user's location changes. -(Void) mapView :( MKMapView *) mapView didUpdateUserLocation :( MKUserLocation *) userLocation {// obtain the latitude and longitude of the current user CLLocationCoordinate2D coordinate = userLocation. location. coordinate; // set the current screen as the center point // [mapView orientation: coordinate animated: YES];/* typedef struct {CLLocationDegrees latitudeDelta; latitude span CLLocationDegrees longitudeDelta; longitude span} longitude; typedef struct {CLLocationCoordinate2D center; center MKCoordinateSpan span;} MKCoordinateRegion; * // set the latitude and longitude span MKCoordinateSpan span = MKCoordinateSpanMake (0.1, 0.1 ); MKCoordinateRegion region = MKCoordinateRegionMake (coordinate, span); // locate the visible area [mapView setRegion: region animated: YES];}