iOS: Load map and add a MKAnnotation

With MapKit, you can do this with something called a “map annotation”. Think of map annotations as the little pins that show up in the Maps app. They don’t necessarily have to be pins – we are gonna make them look something different!
To use annotations there are three steps:
  1. Create a class that implements the MKAnnotation protocol. This means it needs to return a title, subtitle, and coordinate. You can store other information on there if you want too.
  2. For every location you want marked on the map, create one of these classes and add it to the mapView with the addAnnotation method.
  3. Mark the view controller as the map view’s delegate, and for each annotation you added it will call a method on the view controller called mapView:viewForAnnotation:. Your job in this method is to return an instance of MKAnnotationView to present as a visual indicator of the annotation. We’ll be using the base class, MKAnnotationView, in this tutorial but there is a concrete subclass called MKPinAnnotationView which can be used if you want the standard pin you see in the Maps app.
Ok, so let’s start with step 1. Select your ArrestsPlotter group, go to File\New\New File, choose iOS\Cocoa Touch\Objective-C class, and click Next. Enter MyLocation for the class, make it a subclass of NSObject, and finish creating the file.
Replace MyLocation.h with the following:
#import 
#import 
  @interface MyLocation : NSObject   - (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate; - (MKMapItem*)mapItem;   @end
This is a plain old NSObject with a special initializer and a method to return an MKMapItem (more on that in a minute). Note it marks itself as implementing the MKAnnotation protocol. This means that the coordinate property is required, and so are the title and subtitle methods (which we’ll be adding next).
Now replace MyLocation.m with the following:
#import "MyLocation.h"
#import 
 
@interface MyLocation ()
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *address;
@property (nonatomic, assign) CLLocationCoordinate2D theCoordinate;
@end
 
@implementation MyLocation
 
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate {
    if ((self = [super init])) {
        if ([name isKindOfClass:[NSString class]]) {
            self.name = name;
        } else {
            self.name = @"Unknown charge";
        }
        self.address = address;
        self.theCoordinate = coordinate;
    }
    return self;
}
 
- (NSString *)title {
    return _name;
}
 
- (NSString *)subtitle {
    return _address;
}
 
- (CLLocationCoordinate2D)coordinate {
    return _theCoordinate;
}
 
- (MKMapItem*)mapItem {
    NSDictionary *addressDict = @{(NSString*)kABPersonAddressStreetKey : _address};
 
    MKPlacemark *placemark = [[MKPlacemark alloc]
                              initWithCoordinate:self.coordinate
                              addressDictionary:addressDict];
 
    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
    mapItem.name = self.title;
 
    return mapItem;
}
 
@end
Again a simple implementation here – note the required title method (from MKAnnotation protocol) returns the name, and the required subtitle method returns the address. The mapItem method is more interesting. This is creating an instance of a special class called MKMapItem to represent this location. What this class does is provide a way for you to pass information to the Maps app. You’re going to add the ability in a minute to open the Maps app directly from within your app! It’ll show the location in the standard Maps app. Pretty neat!
One more thing you need to do right now is to add the AddressBook framework. Do this like you did before by selecting the ArrestsPlotter project on the left panel of Xcode. Then select the Build Phases tab within the ArrestsPlotter target, open Link Binary with Libraries panel and click the plus button. Finally select AddressBook.framework and select Add. You need this because you’re using kABPersonAddressStreetKey from that framework in the mapItem method.
Onto step 2 – add an instance of one of these classes for every arrest we wish to plot:
// Add to top of file
#import "MyLocation.h"
 
// Load map and add annotation
- (void)viewDidLoad{
    [super viewDidLoad];
    MKCoordinateRegion region;
    CLLocation *locObj = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(_latitude, _longitude) altitude:0 horizontalAccuracy:0 verticalAccuracy:0 timestamp:[NSDate date]];
    region.center = locObj.coordinate;
    MKCoordinateSpan span;
    span.latitudeDelta  = 0.01f; // values for zoom
span.longitudeDelta = 0.01f;
region.span = span;
    [self.mapView setRegion:region animated:YES];
    MyLocation *annotation = [[MyLocation alloc] initWithName:_place address:_address coordinate:CLLocationCoordinate2DMake(_latitude, _longitude)] ;
[_mapView addAnnotation:annotation];
}
iOS: Load map and add a MKAnnotation iOS: Load map and add a MKAnnotation Reviewed by Ricardo Castellanos on 11:34 Rating: 5

No hay comentarios:

Con la tecnología de Blogger.