IOS development map and positioning, ios development map
Use location service: Set the app to have permission to access the location service. First, add the following liang 'ti In the info. plist file.
1. NSLocationWhenInUseUsageDescription during use of the application
2. NSLocationAlwaysUsageDescription always
# Import "ViewController. h"
# Import <CoreLocation/CoreLocation. h> // locate the service
# Import <MapKit/MapKit. h> // map interface
# Import "WXAnnotation. h"
@ Interface ViewController () <MKMapViewDelegate>
{
CLLocationManager * _ manager;
MKMapView * _ mapView;
}
@ End
@ Implementation ViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
// Create an object to manage related positioning services.
_ Manager = [[CLLocationManager alloc] init];
// Manager determines whether the mobile phone has enabled location/whether the app has the permission to access location
// [CLLocationManager locationServicesEnabled]; // whether to enable the Mobile Phone Positioning
// [CLLocationManager authorizationStatus]; // The status of the app's access permission
If (! [CLLocationManager locationServicesEnabled] | [CLLocationManager authorizationStatus]! = Kclthorizationstatusauthorizedwheninuse ){
[_ Manager requestWhenInUseAuthorization]; // request a user to access the positioning service
}
// Create a map
[Self createMapView];
// Create a pin
[Self createAnnotationView];
}
-(Void) createMapView
{
// Create a MapView
_ MapView = [[MKMapView alloc] initWithFrame: self. view. bounds];
// Map type
_ MapView. mapType = MKMapTypeStandard;
// Display the current user location
_ MapView. showsUserLocation = YES;
// Whether to locate (whether to locate the final user)
_ MapView. userTrackingMode = MKUserTrackingModeFollow;
_ MapView. delegate = self;
[Self. view addSubview: _ mapView];
}
// Create a pin
-(Void) createAnnotationView
{
// Create a model object
WXAnnotation * annotation = [[WXAnnotation alloc] init];
Annotation. title = @ "location ";
Annotation. subtitle = @ "Kill! ";
// Coordinate struct
CLLocationCoordinate2D coord = {39.9927359964, 116.3965684639 };
// Coordinates of the pin
Annotation. coordinate = coord;
// Add the annotation to mapView
[_ MapView addAnnotation: annotation];
}
// Update the user's location (proxy method of map view)
-(Void) mapView :( MKMapView *) mapView didUpdateUserLocation :( MKUserLocation *) userLocation
{
// Set MapView Based on latitude and longitude
// Set the display area range and display precision
// Set the display precision (ratio)
MKCoordinateSpan span = MKCoordinateSpanMake (0.01, 0.01 );
// Specified range
MKCoordinateRegion region = MKCoordinateRegionMake (userLocation. location. coordinate, span );
// Move the position displayed by mapView
[_ MapView setRegion: region animated: YES];
}
// Create a proxy for the annotation View
/**
* Call time: annotation must be added to mapView. If the coordinates appear in mapView, call the proxy method to add a pin.
*/
-(MKAnnotationView *) mapView :( MKMapView *) mapView viewForAnnotation :( id <MKAnnotation>) annotation
{
// The location of the current user. No pin is displayed.
If ([annotation isKindOfClass: [MKUserLocation class]) {
Return nil;
}
// MKAnnotationView cannot be used directly (custom pin view, inherited)
// The Pin view displayed by MKPinAnnotationView
Static NSString * iden = @ "Annotation_view ";
// Pin View
MKPinAnnotationView * annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: iden];
If (annotationView = nil ){
AnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: iden];
// Pin color
AnnotationView. pinColor = MKPinAnnotationColorPurple;
// Animation with a pin falling from the sky
AnnotationView. animatesDrop = YES;
// Display content
AnnotationView. canShowCallout = YES;
// Secondary View
AnnotationView. rightCalloutAccessoryView = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
}
// Reset the model of the pin view to solve the reuse problem.
AnnotationView. annotation = annotation;
Return annotationView;
}