views:

44

answers:

2

hi i am working with Mapkit and i have to show annotation in the map but am not able to display the annotation hers my code plz tell me where i am going wrong

@interface MyMapView : UIViewController <MKAnnotation,MKMapViewDelegate>{

MKMapView *Obj_Map_View;
MKPlacemark *pmark;
MKReverseGeocoder *geocoder1;

}

@end

#import "MyMapView.h"

@implementation MyMapView

  • (id)init { if (self = [super init]) {

    } return self; }

  • (void)loadView {
    [super loadView];
    Obj_Map_View = [[MKMapView alloc]initWithFrame:self.view.bounds]; Obj_Map_View.showsUserLocation =YES; Obj_Map_View.mapType=MKMapTypeStandard; [self.view addSubview:Obj_Map_View]; Obj_Map_View.delegate = self;

    CLLocationCoordinate2D cord = {latitude: 19.120000, longitude: 73.020000}; MKCoordinateSpan span = {latitudeDelta:0.3, longitudeDelta:0.3}; MKCoordinateRegion reg= {cord,span}; [Obj_Map_View setRegion:reg animated:YES]; //[Obj_Map_View release]; }

  • (NSString *)subtitle{ return @"Sub Title"; }

  • (NSString *)title{ return @"Title"; }

  • (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) annotation { MKPinAnnotationView *annov = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"Current location"]; annov.animatesDrop = TRUE; [annotation title]==@"Current location"; annov.canShowCallout = YES; [annov setPinColor:MKPinAnnotationColorGreen]; return annov; }

The above code works fine and displays a map but not with annotation, so plz tell me where am going wrong.... Thank You

A: 

Typically, the class that conforms to the MKAnnotation protocol isn't the view controller, it's a data class.

You'll need to create another class, which I'll call "MyLandmarks" for the example.

@interface MyLandmarks : NSObject <MKAnnotation>
   // Normally, there'd be some variables that contain the name and location.
   // And maybe some means to populate them from a URL or a database.
   // This example hard codes everything.

@end


@implementation MyLandmarks
-(NSString*)title {
    return @"'ere I am, J.H.";
}   

-(NSString*)subtitle {
    return @"The ghost in the machine.";
}

-(CLLocationCoordinate2D) coordinate {
    CLLocationCoordinate2D coord = {latitude: 19.120000, longitude: 73.020000}; 

    return coord; 
}
@end

Then, somewhere appropriate in your MyMapView class add:

MyLandmark *landmark = [[[MyLandmark alloc]init]autorelease];
[Obj_Map_View addAnnotation:landmark];

A couple other bits that other Objective-C developers working with you will appreciate:

  • To avoid confusion, don't call the class MyMapView if it descends from a UIViewController. Call it MyMapViewController, instead.
  • Classes start with a capital letter, variables start lowercase. Both are CamelCased. Obj_Map_View should be objMapView.
John Franklin
Thanks John it worked
Radix
A: 

To add annotation use : addAnnotation:

read about it here

Idan