Friday, 26 June 2015

MapView With MkAnnotation

ViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface ViewController : UIViewController<MKMapViewDelegate>
{
    IBOutlet MKMapView *mapview;
}
- (IBAction)userlocation:(id)sender;

//- (IBAction)backahmedabad:(id)sender;

@end


ViewController.m

#import "ViewController.h"
#import "MkAnnotation.h"

@interface ViewController ()
{
    NSArray *annotations;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    mapview.delegate = self;
    mapview.showsUserLocation = YES;
    [self addAnnotations];
   
}

-(void) addAnnotations {
    MkAnnotation *annotation1 = [[MkAnnotation alloc]initWithTitle:@"Nehrunagar" subTitle:@"Ahmedabad" andCoordinates:CLLocationCoordinate2DMake(23.022479000000000000, 72.542987199999970000)];
    annotation1.tag = 1;
   
    MkAnnotation *annotation2 = [[MkAnnotation alloc]initWithTitle:@"Income Tax" subTitle:@"Ahmedabad" andCoordinates:CLLocationCoordinate2DMake(23.039567700000000000, 72.566004499999960000)];
    annotation2.tag = 2;
   
    annotations = @[annotation1,annotation2];
    [mapview addAnnotation:annotation1];
    [mapview showAnnotations:annotations animated:YES];
}

- (MKPinAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if([annotation isKindOfClass:[MKUserLocation class]]){
        return nil;
    }
    static NSString *identifier = @"identifier";
    MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:identifier ];
    MkAnnotation *ann = (MkAnnotation *)annotation;
    if(pinAnnotation == nil)
    {
        pinAnnotation = [[MKPinAnnotationView alloc]initWithAnnotation:ann reuseIdentifier:identifier];
    }
    else
        pinAnnotation.annotation = ann;
    if(ann.tag ==1)
    {
        pinAnnotation.pinColor = MKPinAnnotationColorGreen;
    }
    else if(ann.tag == 2){
        pinAnnotation.pinColor = MKPinAnnotationColorPurple;
    }
   
    pinAnnotation.canShowCallout =YES;
    pinAnnotation.draggable = YES;
    return pinAnnotation;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)userlocation:(id)sender
{
    [mapview setCenterCoordinate:mapview.userLocation.coordinate animated:YES];
}

- (IBAction)backahmedabad:(id)sender {
    MkAnnotation *ann1 = (MkAnnotation *)[annotations firstObject];
    [mapview setCenterCoordinate:ann1.coordinate animated:YES ];
}
@end


MkAnnotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MkAnnotation : NSObject<MKAnnotation>

@property(nonatomic,readwrite)NSInteger *tag;
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *subtitle;
@property(nonatomic,readwrite)CLLocationCoordinate2D coordinate;

-(id)initWithTitle:(NSString *)title subTitle:(NSString *)subTitle andCoordinates:(CLLocationCoordinate2D)coordinate;
@end


MkAnnotation.m

#import "MkAnnotation.h"

@implementation MkAnnotation
@synthesize tag;
@synthesize title = _title;
@synthesize subtitle = _subtitle;
@synthesize coordinate = _coordinate;
-(id)initWithTitle:(NSString *)title subTitle:(NSString *)subTitle andCoordinates:(CLLocationCoordinate2D)coordinate;{

    self  = [super init];
    if(self){
        _title = title;
        _subtitle = subTitle;
        _coordinate = coordinate;
    }
    return self;
}
@end

No comments:

Post a Comment