A positioning demo and a positioning demo

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.