Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @interface MyAnnotationClass : NSObject<MKAnnotation> {
- NSString *_name;
- NSString *_description;
- CLLocationCoordinate2D _coordinate;
- }
- @property (nonatomic, retain) NSString *name;
- @property (nonatomic, retain) NSString *description;
- @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- -(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate;
- - (NSString *)subtitle;
- - (NSString *)title;
- -(void)setTitle:(NSString*)strTitle;
- -(void)setSubTitle:(NSString*)strSubTitle;
- @end
- MyAnnotationClass.m
- #import "MyAnnotationClass.h"
- @implementation MyAnnotationClass
- @synthesize name = _name;
- @synthesize description = _description;
- @synthesize coordinate = _coordinate;
- -(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate{
- self=[super init];
- if(self){
- _coordinate=coordinate;
- }
- return self;
- }
- -(void)setTitle:(NSString*)strTitle {
- self.name = strTitle;
- }
- -(void)setSubTitle:(NSString*)strSubTitle {
- self.description = strSubTitle;
- }
- -(void) dealloc{
- // self.name = nil;
- // self.description = nil;
- [super dealloc];
- }
- @end
- ViewController.h
- #import <UIKit/UIKit.h>
- #import <MapKit/MapKit.h>
- @interface ViewController : UIViewController<MKMapViewDelegate> {
- IBOutlet MKMapView *_myMapView;
- NSArray *_myAnnotations;
- }
- @property (nonatomic, retain) NSArray *myAnnotations;
- @property (nonatomic, retain) IBOutlet MKMapView *myMapView;
- @end
- ViewController.m
- #import "ViewController.h"
- #import "AppDelegate.h"
- #import <MapKit/MapKit.h>
- #import <CoreLocation/CoreLocation.h>
- #import "PlaceMark.h"
- #import "MyAnnotationClass.h"
- @interface ViewController ()
- @end
- @implementation ViewController
- @synthesize myMapView = _myMapView;
- @synthesize myAnnotations = _myAnnotations;
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- -(void) viewDidLoad{
- AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
- //Initialize annotation
- MyAnnotationClass *commuterLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake(appDelegate.latitude , appDelegate.longitude)];
- MyAnnotationClass *overflowLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake(appDelegate.latitude , appDelegate.longitude)];
- //Add them to array
- self.myAnnotations=[NSArray arrayWithObjects:commuterLotAnnotation, overflowLotAnnotation, nil];
- //Release the annotations now that they've been added to the array
- [commuterLotAnnotation release];
- [overflowLotAnnotation release];
- //add array of annotations to map
- [_myMapView addAnnotations:_myAnnotations];
- }
- -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{
- static NSString *parkingAnnotationIdentifier=@"ParkingAnnotationIdentifier";
- if([annotation isKindOfClass:[MyAnnotationClass class]]){
- //Try to get an unused annotation, similar to uitableviewcells
- MKAnnotationView *annotationView=[_myMapView dequeueReusableAnnotationViewWithIdentifier:parkingAnnotationIdentifier];
- [annotation setTitle:@"Test name"];
- [annotation setSubTitle:@"test Description"];
- //If one isn't available, create a new one
- if(!annotationView){
- annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:parkingAnnotationIdentifier];
- //Here's where the magic happens
- annotationView.image=[UIImage imageNamed:@"apple.gif"];
- }
- return annotationView;
- }
- return nil;
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement