iOS positioning plus pins

Links to the original text: https://my.oschina.net/zyboy/blog/617431

Location of Map Combination
1. Same part as single positioning
IOS 8 + Must Ask User's Consent
2. Different Parts with Simple Location
Protocol: MKMapViewDelegate (MapKit Framework)
b. Class/Control: Map Control MKMapView
c. Location is also different: through an attribute of a map view
Details:
If you drag an MKMapView into storyboard manually, you need to import the MapKit Framework manually.
b. If you add MKMapView in pure code, step a is omitted
C. The CLLocation Manager object declares an attribute and does not cause the pop-up box to be retrieved quickly (!!!!!!).

viewController.m

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "TRAnnotation.h"

@interface ViewController ()<MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

//CLLocationManager
@property (nonatomic,strong) CLLocationManager *mgr;

//CLGeocoder
@property (nonatomic, strong) CLGeocoder *geocoder;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.mgr = [CLLocationManager new];
    self.geocoder = [CLGeocoder new];

    //Consult users (Info.plist)
    [self.mgr requestWhenInUseAuthorization];

    //Setting agent
    self.mapView.delegate = self;

    //Setting Map Properties
    self.mapView.rotateEnabled = YES;

    //Set up the display mode of map (satellite/satellite hybrid)
    self.mapView.mapType = MKMapTypeStandard;

    //Perform positioning operations
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
}

#pragma mark --- MapView Delegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    //Using anti geocoding to obtain detailed information about user location (country / Province)
    [self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
        //Use the information given by placemark to set the text information of the user's location pin
        CLPlacemark *placemark = [placemarks lastObject];
        //title
        userLocation.title = placemark.name;

        //subtitle
        userLocation.subtitle = placemark.locality;
    }];

    //Display map
    MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);
    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
    [self.mapView setRegion:region animated:YES];
}


//Map Drag Call Method
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

    NSLog(@"span: %f; %f", self.mapView.region.span.latitudeDelta, self.mapView.region.span.longitudeDelta);
}

//The Agent Method that Customizes the Needle Must Be Implemented
//1. How many pins are there on the map and how many times are they called?
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    NSLog(@"Customized pin method");
    //6. If nil is returned by default, the current user location is the default blue; the other defaults are red.
// return nil;
    //Exclude the type of pin that the user's location belongs to
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        //The current big head for an image annotation is the user's location
        return nil;//blue
    }

    //2. Use reuse mechanisms
    //2.1 Declare a reusable pin identifier
    static NSString *identifier = @"Annotation";
    //2.2 Get reusable large head-on objects from the buffer pool, and if so, put them back directly.
    MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    //2.3 If not in the buffer pool, reproduce the creation of large head-to-object
    if (!pinAnnotation) {
        //3. The first parameter must be nil(??????)
        pinAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];
        //4. You must set an attribute to display the pop-up box (otherwise title/subtitle cannot be displayed)
        pinAnnotation.canShowCallout = YES;

        //Setting Left and Right Auxiliary View of Big Pin
        pinAnnotation.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_classify_cafe"]];
        pinAnnotation.rightCalloutAccessoryView = [[UISwitch alloc] init];
    }

    //Setting pin pin properties (iOS9 will have an additional custom color property)
    pinAnnotation.pinColor = MKPinAnnotationColorPurple;
    //7. The animation and image settings of the pin view cannot be set at the same time.
// pinAnnotation.animatesDrop = YES;
    //Set up a picture of a pin
    pinAnnotation.image = [UIImage imageNamed:@"icon_classify_cafe"];

    //5. Assign the Parameter annotation in the proxy method to the pinAnnoation view object
    pinAnnotation.annotation = annotation;

    return pinAnnotation;
}

//Manually add two pins
- (IBAction)addAnnotation:(id)sender {

    //Create two custom large head-to-object
    TRAnnotation *firstAnnotation = [[TRAnnotation alloc] init];
    TRAnnotation *secondAnnotation = [[TRAnnotation alloc] init];

    //Set the properties for adding pins (location / title Text / subtitle sub-text)
    firstAnnotation.coordinate = CLLocationCoordinate2DMake(39.875, 116.456);
    firstAnnotation.title = @"iOS1506";
    firstAnnotation.subtitle = @"Fighting";

    secondAnnotation.coordinate = CLLocationCoordinate2DMake(39.879, 116.451);
    secondAnnotation.title = @"iOS1506";
    secondAnnotation.subtitle = @"AZAZA";

    //Add to the map
    [self.mapView addAnnotation:firstAnnotation];
    [self.mapView addAnnotation:secondAnnotation];

    //Set up the area (center/span) shown on the map
    MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
    MKCoordinateRegion region = MKCoordinateRegionMake(secondAnnotation.coordinate, span);
    [self.mapView setRegion:region animated:YES];
}
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface TRAnnotation : NSObject <MKAnnotation>

//Location (required must)
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

//title(optional optional)
@property (nonatomic, copy) NSString *title;

//subtitle(optional optional)
@property (nonatomic, copy) NSString *subtitle;

@end

Steps for adding custom pins (Mode 1)
1. Create a custom class (a.b.c)
2. The Method of Implementing Agent
- (MKAnnotationView )mapView:(MKMapView )mapView viewForAnnotation:(id)annotation {}
3. In the second step, the proxy method performs the operation.
a. First, exclude the classes corresponding to the big head of the user.
b. Next, create the reuse mechanism of the pin manually (similar to table view cell)
c. Bighead Pin View Conversion Type MKPinAnnotationView Obtained from Cache Pool
d. Setting the properties of the pin (color, animation/picture)

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "TRAnnotation.h"

@interface ViewController ()<MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

//CLLocationManager
@property (nonatomic,strong) CLLocationManager *mgr;

//CLGeocoder
@property (nonatomic, strong) CLGeocoder *geocoder;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.mgr = [CLLocationManager new];
    self.geocoder = [CLGeocoder new];

    //Consult users (Info.plist)
    [self.mgr requestWhenInUseAuthorization];

    //Setting agent
    self.mapView.delegate = self;

    //Setting Map Properties
    self.mapView.rotateEnabled = YES;

    //Set up the display mode of map (satellite/satellite hybrid)
    self.mapView.mapType = MKMapTypeStandard;

    //Perform positioning operations
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
}

#pragma mark --- MapView Delegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    //Using anti geocoding to obtain detailed information about user location (country / Province)
    [self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
        //Use the information given by placemark to set the text information of the user's location pin
        CLPlacemark *placemark = [placemarks lastObject];
        //title
        userLocation.title = placemark.name;

        //subtitle
        userLocation.subtitle = placemark.locality;
    }];

    //Display map
    MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);
    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
    [self.mapView setRegion:region animated:YES];
}


//Map Drag Call Method
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

    NSLog(@"span: %f; %f", self.mapView.region.span.latitudeDelta, self.mapView.region.span.longitudeDelta);
}

//The Agent Method that Customizes the Needle Must Be Implemented
//1. How many pins are there on the map and how many times are they called?
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    NSLog(@"Customized pin method");
    //6. If nil is returned by default, the current user location is the default blue; the other defaults are red.
// return nil;
    //Exclude the type of pin that the user's location belongs to
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        //The current big head for an image annotation is the user's location
        return nil;//blue
    }

    //2. Use reuse mechanisms
    //2.1 Declare a reusable pin identifier
    static NSString *identifier = @"Annotation";
    //2.2 Get reusable large head-on objects from the buffer pool, and if so, put them back directly.
    MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    //2.3 If not in the buffer pool, reproduce the creation of large head-to-object
    if (!pinAnnotation) {
        //3. The first parameter must be nil(??????)
        pinAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];
        //4. You must set an attribute to display the pop-up box (otherwise title/subtitle cannot be displayed)
        pinAnnotation.canShowCallout = YES;

        //Setting Left and Right Auxiliary View of Big Pin
        pinAnnotation.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_classify_cafe"]];
        pinAnnotation.rightCalloutAccessoryView = [[UISwitch alloc] init];
    }

    //Setting pin pin properties (iOS9 will have an additional custom color property)
    pinAnnotation.pinColor = MKPinAnnotationColorPurple;
    //7. The animation and image settings of the pin view cannot be set at the same time.
// pinAnnotation.animatesDrop = YES;
    //Set up a picture of a pin
    pinAnnotation.image = [UIImage imageNamed:@"icon_classify_cafe"];

    //5. Assign the Parameter annotation in the proxy method to the pinAnnoation view object
    pinAnnotation.annotation = annotation;

    return pinAnnotation;
}

//Manually add two pins
- (IBAction)addAnnotation:(id)sender {

    //Create two custom large head-to-object
    TRAnnotation *firstAnnotation = [[TRAnnotation alloc] init];
    TRAnnotation *secondAnnotation = [[TRAnnotation alloc] init];

    //Set the properties for adding pins (location / title Text / subtitle sub-text)
    firstAnnotation.coordinate = CLLocationCoordinate2DMake(39.875, 116.456);
    firstAnnotation.title = @"iOS1506";
    firstAnnotation.subtitle = @"Fighting";

    secondAnnotation.coordinate = CLLocationCoordinate2DMake(39.879, 116.451);
    secondAnnotation.title = @"iOS1506";
    secondAnnotation.subtitle = @"AZAZA";

    //Add to the map
    [self.mapView addAnnotation:firstAnnotation];
    [self.mapView addAnnotation:secondAnnotation];

    //Set up the area (center/span) shown on the map
    MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
    MKCoordinateRegion region = MKCoordinateRegionMake(secondAnnotation.coordinate, span);
    [self.mapView setRegion:region animated:YES];
}

Two ways to create custom pins
1. Use MK Annotation View
2. Create custom headers for different objects (add a picture attribute)
3. The realization logic of agent is different

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "TRAnnotaion.h"

@interface ViewController ()<MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

//CLLocationManager
@property (nonatomic,strong) CLLocationManager *mgr;

//CLGeocoder
@property (nonatomic, strong) CLGeocoder *geocoder;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.mgr = [CLLocationManager new];
    self.geocoder = [CLGeocoder new];

    //Consult users (Info.plist)
    [self.mgr requestWhenInUseAuthorization];

    //Setting agent
    self.mapView.delegate = self;

    //Setting Map Properties
    self.mapView.rotateEnabled = YES;
    //Set up the display mode of map (satellite/satellite hybrid)

    //Perform positioning operations
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
}



#pragma mark --- MapView Delegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    //Using anti geocoding to obtain detailed information about user location (country / Province)
    [self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
        //Use the information given by placemark to set the text information of the user's location pin
        CLPlacemark *placemark = [placemarks lastObject];
        //title
        userLocation.title = placemark.name;

        //subtitle
        userLocation.subtitle = placemark.locality;
    }];

    //Display map
    MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);
    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
    [self.mapView setRegion:region animated:YES];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    //Eliminate the pin corresponding to the user's position
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;//Blue circle
    }

    //Reuse mechanism
    static NSString *identifier = @"Annotation";
    //No conversion
    MKAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (!annotationView) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];
        //Display title/subtitile attributes
        annotationView.canShowCallout = YES;
    }

    //Differences between Mode 2 (MK AnnotionaView) and Mode 1 (MK Pin Annotation View)
    TRAnnotaion *anno = (TRAnnotaion *)annotation;
    annotationView.annotation = anno;
    annotationView.image = anno.image;

    return annotationView;
}

- (IBAction)addAnnotation:(id)sender {

    //Creating Two Bighead Targeted Objects
    TRAnnotaion *firstAnnoation = [[TRAnnotaion alloc] init];
    TRAnnotaion *secondAnnotion = [[TRAnnotaion alloc] init];
    firstAnnoation.coordinate = CLLocationCoordinate2DMake(39.875, 116.342);
    firstAnnoation.title = @"iOS1506";
    firstAnnoation.subtitle = @"Fighting";
    //Different
    firstAnnoation.image = [UIImage imageNamed:@"icon_paopao_waterdrop_streetscape"];

    secondAnnotion.coordinate = CLLocationCoordinate2DMake(39.877, 116.457);
    secondAnnotion.title = @"iOS1506";
    secondAnnotion.subtitle = @"AZAZA";
    //Different
    secondAnnotion.image = [UIImage imageNamed:@"icon_pin_floating"];

    //Add to the map; set the area of the map (center/span)
    [self.mapView addAnnotation:firstAnnoation];
    [self.mapView addAnnotation:secondAnnotion];

    MKCoordinateSpan span = MKCoordinateSpanMake(0.2, 0.2);
    MKCoordinateRegion region = MKCoordinateRegionMake(firstAnnoation.coordinate, span);
    [self.mapView setRegion:region animated:YES];   
}

Reproduced in: https://my.oschina.net/zyboy/blog/617431

Keywords: Attribute iOS

Added by marcth on Sat, 05 Oct 2019 19:01:57 +0300