Issue with Annotation Views disappearing on double tap zoom - iphone

I've ran into a problem with annotation views in the MapKit on the iPhone. I manage to draw custom annotation views on the map - no problem there. I even manage to redraw them after dragging or zooming. However, there are cases where the redrawing does not work: an example would be double-tap zoom.
I attach some code where I draw a few rectangles at specific locations on the map, and when I zoom using a two finger gesture, everything works fine (i.e. the rectangles are redrawn). However, when I double tap, the rectangles disappear. What's even stranger is that all methods get called in the order that they should, and in the end, even the drawRect gets called - but the rectangles are not drawn.
So here's the code, please try for yourself - two finger zooming works, but double-tap zooming doesn't:
PlaygroundViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface PlaygroundViewController : UIViewController <MKMapViewDelegate>{
MKMapView *mapView_;
NSMutableDictionary* myViews_;
}
#end
PlaygroundViewController.m
#import "PlaygroundViewController.h"
#import "Territory.h"
#import "TerritoryView.h"
#implementation PlaygroundViewController
- (void)viewDidLoad {
[super viewDidLoad];
mapView_=[[MKMapView alloc] initWithFrame:self.view.bounds];
[self.view insertSubview:mapView_ atIndex:0];
mapView_.delegate = self;
[mapView_ setMapType:MKMapTypeStandard];
[mapView_ setZoomEnabled:YES];
[mapView_ setScrollEnabled:YES];
myViews_ = [[NSMutableDictionary alloc] init];
for (int i = 0; i < 10; i++ ) {
Territory *territory;
territory = [[[Territory alloc] init] autorelease];
territory.latitude_ = 40 + i;
territory.longitude_ = -122 + i;
[mapView_ addAnnotation:territory];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView* territoryView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"Territory"];
if (!territoryView){
territoryView = [[[TerritoryView alloc] initWithAnnotation:annotation reuseIdentifier:#"Territory"] autorelease];
Territory* currentTerritory = (Territory*) annotation;
[myViews_ setObject:territoryView forKey:currentTerritory.territoryID_];
}
else{
territoryView.annotation = annotation;
}
return territoryView;
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
for (NSObject* key in [myViews_ allKeys]) {
TerritoryView* territoryView = [myViews_ objectForKey:key];
[territoryView initRedraw];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[super dealloc];
}
Territory.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface Territory : NSObject <MKAnnotation> {
float latitude_;
float longitude_;
NSString* territoryID_;
}
#property (nonatomic) float latitude_;
#property (nonatomic) float longitude_;
#property (nonatomic, retain) NSString* territoryID_;
#end
Territory.m
#import "Territory.h"
#implementation Territory
#synthesize latitude_;
#synthesize longitude_;
#synthesize territoryID_;
- (CLLocationCoordinate2D)coordinate {
CLLocationCoordinate2D coord_ = {self.latitude_, self.longitude_};
return coord_;
}
-(id) init {
if (self = [super init]) {
self.territoryID_ = [NSString stringWithFormat:#"%p", self];
}
return self;
}
#end
TerritoryView.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface TerritoryView : MKAnnotationView {
}
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
- (void)initRedraw;
#end
TerritoryView.m
#import "TerritoryView.h"
#implementation TerritoryView
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
if ([super initWithAnnotation:annotation reuseIdentifier:#"Territory"]) {
self.initRedraw;
}
return self;
}
- (void)initRedraw {
self.frame = CGRectMake(0,0,40,40);
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
NSLog(#"in draw rect");
}
#end
Any help is appreciated. Here's the zipped project: link

Keep in mind that the frame's origin is in the coordinate system of its parent, so setting it to zero is probably putting it off screen. I suspect that the reason it ever works at all is the it's getting reset behind your back in most situations, but not in those where it's failing.
replace:
self.frame = CGRectMake(0,0,40,40);
with:
self.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y,40,40);

Related

[PointInfoController endAppearanceTransition]: message sent to deallocated instance 0x74d5e90

I need a little help here, I've loaded a view by modal and basically I'm now trying to dismiss this view now but it keep crashing after the view goes.
It says * -[PointInfoController endAppearanceTransition]: message sent to deallocated instance 0x74d5e90
Below is the .H file:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#class PointInfoController;
#protocol PointInfoDelegate
- (void)PointInfoDidFinish:(PointInfoController*)PointInfoController;
#end
#interface PointInfoController : UIViewController <UIScrollViewDelegate>
{
IBOutlet UIScrollView *scrollView;
IBOutlet UIImageView *thumbnailView;
IBOutlet UIButton *Dismiss;
id<PointInfoDelegate> delegate;
}
#property (retain) id<PointInfoDelegate> delegate;
#property (retain, nonatomic) IBOutlet UITextView *description;
#property (retain, nonatomic) IBOutlet UIScrollView *scrollView;
#property (retain, nonatomic) IBOutlet UIImageView *thumbnailView;
- (IBAction)DismissView:(id)sender;
#end
Below is the .M file:
#import "PointInfoController.h"
#import "LockOnLocation.h"
#implementation UIScrollView (AutoContentSize)
- (void) setAutosizeContent:(BOOL)autosizeContent {
if (autosizeContent) {
CGFloat contentWidth =
self.frame.size.width == self.superview.frame.size.width ?
self.superview.frame.size.width :
self.frame.size.width + 10;
CGFloat contentHeight =
self.frame.size.height == self.superview.frame.size.height ?
self.superview.frame.size.height :
self.frame.size.height + 164;
self.contentSize = CGSizeMake(contentWidth, contentHeight);
}
}
#end
#implementation PointInfoController
#synthesize description;
#synthesize scrollView;
#synthesize thumbnailView;
#synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
self.scrollView.delegate = self;
[super viewDidLoad];
}
- (void)viewDidUnload
{
[self setDescription:nil];
[scrollView release];
scrollView = nil;
[self setScrollView:nil];
[self setThumbnailView:nil];
[thumbnailView release];
thumbnailView = nil;
[Dismiss release];
Dismiss = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (IBAction)DismissView:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
[self release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[description release];
[scrollView release];
[thumbnailView release];
[Dismiss release];
[super dealloc];
}
#end
I'm new to all this and I spent quite a few days on the same problem. Blamed everything but myself! I discovered I was inadvertently release(ing) the instance referred to. In my case I had put the instance into a SSLConnectionRef and CFRelease'd it. I'm not sure why you are having the issue looking at your code. In any event, I thought your compiler would complain about your [instance release] methods, since these are no longer allowed. You are having the problem because both you and the system are releasing the instance. You need to get rid of your release and all will be well.

MapKit mapView:viewForAnnotation has no effect on pin color

I can't seem to change the pin color.
I have my view controller extend <MKMapViewDelegate> and implement mapView:viewForAnnotation I'm close but must be missing something. any help would be appreciated.
MainViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "StopAnnotation.h"
#define METERS_PER_MILE 1609.344
#interface MainViewController : UIViewController <MKMapViewDelegate> {
}
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#end
MapViewController.m
#import "MainViewController.h"
#implementation MainViewController
#synthesize mapView=_mapView;
- (void)viewWillAppear:(BOOL)animated
{
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 43.066667;
zoomLocation.longitude = -89.4;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, METERS_PER_MILE, METERS_PER_MILE);
MKCoordinateRegion adjustRegion = [_mapView regionThatFits:viewRegion];
[_mapView setRegion:adjustRegion animated:YES];
[_mapView addAnnotation:[[StopAnnotation alloc] initWithCoordinate:zoomLocation]];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKPinAnnotationView *pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
pav.pinColor = MKPinAnnotationColorPurple;
return pav;
}
// the rest of the methods are default, i.e. viewDid* and shouldAutorotate*, etc...
StopAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface StopAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)c;
#end
StopAnnotation.m
#import "StopAnnotation.h"
#implementation StopAnnotation
#synthesize coordinate;
- (NSString *)subtitle {
return #"subtitle";
}
- (NSString *)title {
return #"title";
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)c {
coordinate = c;
NSLog(#"%f,%f", c.latitude, c.longitude);
return self;
}
#end
I'm doing an exercise & the code was mostly from here
Thanks!!
You must set MainViewController as the delegate of mapView, ie if you are not then the map view might be generating default pins, if you put a breakpoint in
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
does it actually get called?

my delegate doesn't fire after NSXMLParser loaded

i have a class that work as xml parser , am trying to create a delegate to fill in my tableView with data after parserDidEndDocument called ,
my parser class :
DataPareser.h
#import <Foundation/Foundation.h>
#import "resturantModel.h"
#protocol OnPareserDoneDelegate <NSObject>
#required
-(void) dataFormParser:(NSMutableArray *) itemsArray;
#end
#interface DataPareser : NSObject <NSXMLParserDelegate>{
resturantModel * res;
NSString * currentElementName ;
NSString * currentString ;
NSMutableString * name ;
NSString * city ;
NSString * street ;
NSString * phone1 ;
NSString * phone2 ;
NSString * phone3 ;
NSString * phone4 ;
int ID;
id<OnPareserDoneDelegate>onPareserDoneDelegate;
}
#property (nonatomic, assign) id onPareserDoneDelegate;
- (void)loadUrl :(NSString *)url;
#end
DataPareser.m
import "DataPareser.h"
#implementation DataPareser
#synthesize onPareserDoneDelegate;
static NSMutableArray * itemsArray;
- (void)loadUrl:(NSString *)url{
NSURL* nsURL=[[NSURL alloc]initWithString:url];
NSXMLParser* parser=[[NSXMLParser alloc]initWithContentsOfURL:nsURL];
[parser setDelegate:self];
itemsArray=[[NSMutableArray alloc]init ];
BOOL success = [parser parse];
if(success)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
//NSLog(#"Number of Resturants Found : %d",[itemsArray count]);
if([itemsArray count]>0){
NSLog(#"parserDidEndDocument");
NSLog(#"%d",[itemsArray count]);
[[self onPareserDoneDelegate] dataFormParser:itemsArray];
}else {
}
[name release];
[city release];
[street release];
[phone1 release];
[phone2 release];
[phone3 release];
[phone4 release];
}
#end
and i used my delegate on my ResultViewController
ResultViewController.h
#import "resturantModel.h"
#import "SearchViewController.h"
#import "DataPareser.h"
#interface ResultsViewController : UIViewController <OnPareserDoneDelegate,UITableViewDelegate,UITableViewDataSource> {
IBOutlet UINavigationController * navController;
IBOutlet UITableView * table;
NSMutableArray * array;
resturantModel * res;
NSMutableArray *items;
}
#property (nonatomic,retain)IBOutlet UINavigationController * navController;
#property (nonatomic,retain)IBOutlet UITableView * table;
#end
ResultsViewController.m
#import "ResultsViewController.h"
#implementation ResultsViewController
#synthesize table;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSLog(#"initWithNibName");
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[navController release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
NSLog(#"Result Did Load");
table=[[UITableView alloc]init];
table.delegate=self;
[table reloadData];
[self.view addSubview:navController.view];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark OnPareserDoneDelegate deleget methodes
-(void) dataFormParser:(NSMutableArray *) itemsArray {
NSLog(#"dataFormParser");
}
#end
in my app , am invoking the parser and it run well but the problem that the delegate doesn't fire ?!
I can't see where you are actually using your parser class?
At some point, possibly in ResultsViewController (depending on how you are structuring things) you would need something like:
DataPareser *dataParser = [[DataPareser alloc] init]; // Create an instance of parser
dataParser.onPareserDoneDelegate = self; // Set the delegate to this class
[dataParser loadUrl:urlToLoad]; // Start doing your work
From the source provided this is only a best guess

How to add annotation on center of map view in iPhone?

I have MAP view in my app and i want to add Annotation pin (Red pin) on center of map view.
Now when user scroll map view pin should adjust with center according to that.
How to do that?
Thank
If you want to use an actual annotation instead of just a regular view positioned above the center of the map view, you can:
use an annotation class with a settable coordinate property (pre-defined MKPointAnnotation class eg). This avoids having to remove and add the annotation when the center changes.
create the annotation in viewDidLoad
keep a reference to it in a property, say centerAnnotation
update its coordinate (and title, etc) in the map view's regionDidChangeAnimated delegate method (make sure map view's delegate property is set)
Example:
#interface SomeViewController : UIViewController <MKMapViewDelegate> {
MKPointAnnotation *centerAnnotation;
}
#property (nonatomic, retain) MKPointAnnotation *centerAnnotation;
#end
#implementation SomeViewController
#synthesize centerAnnotation;
- (void)viewDidLoad {
[super viewDidLoad];
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = mapView.centerCoordinate;
pa.title = #"Map Center";
pa.subtitle = [NSString stringWithFormat:#"%f, %f", pa.coordinate.latitude, pa.coordinate.longitude];
[mapView addAnnotation:pa];
self.centerAnnotation = pa;
[pa release];
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
centerAnnotation.coordinate = mapView.centerCoordinate;
centerAnnotation.subtitle = [NSString stringWithFormat:#"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude];
}
- (void)dealloc {
[centerAnnotation release];
[super dealloc];
}
#end
Now this will move the annotation but not smoothly. If you need the annotation to move more smoothly, you can add a UIPanGestureRecognizer and UIPinchGestureRecognizer to the map view and also update the annotation in the gesture handler:
// (Also add UIGestureRecognizerDelegate to the interface.)
// In viewDidLoad:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
panGesture.delegate = self;
[mapView addGestureRecognizer:panGesture];
[panGesture release];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
pinchGesture.delegate = self;
[mapView addGestureRecognizer:pinchGesture];
[pinchGesture release];
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
centerAnnotation.coordinate = mapView.centerCoordinate;
centerAnnotation.subtitle = [NSString stringWithFormat:#"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//let the map view's and our gesture recognizers work at the same time...
return YES;
}
Anna's answer is clever approach to keeping an annotation centered in the map using the annotations in the standard mapview way. However, as one commenter pointed out, the scrolling and pinching while much better still shows noticeable lag, and the recommended approach would be to add the annotation view as a subview of the mapview. Here's what that looks like.
#interface SHCenterPinMapViewController () <MKMapViewDelegate>
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#property (strong, nonatomic) MKPointAnnotation *centerAnnotaion;
#property (strong, nonatomic) MKPinAnnotationView *centerAnnotationView;
#end
#implementation SHCenterPinMapViewController
- (MKPointAnnotation *)centerAnnotaion
{
if (!_centerAnnotaion) {
_centerAnnotaion = [[MKPointAnnotation alloc] init];
}
return _centerAnnotaion;
}
- (MKPinAnnotationView *)centerAnnotationView
{
if (!_centerAnnotationView) {
_centerAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:self.centerAnnotaion
reuseIdentifier:#"centerAnnotationView"];
}
return _centerAnnotationView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
[self.mapView addSubview:self.centerAnnotationView];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self moveMapAnnotationToCoordinate:self.mapView.centerCoordinate];
}
// These are the constants need to offset distance between the lower left corner of
// the annotaion view and the head of the pin
#define PIN_WIDTH_OFFSET 7.75
#define PIN_HEIGHT_OFFSET 5
- (void)moveMapAnnotationToCoordinate:(CLLocationCoordinate2D) coordinate
{
CGPoint mapViewPoint = [self.mapView convertCoordinate:coordinate toPointToView:self.mapView];
// Offset the view from to account for distance from the lower left corner to the pin head
CGFloat xoffset = CGRectGetMidX(self.centerAnnotationView.bounds) - PIN_WIDTH_OFFSET;
CGFloat yoffset = -CGRectGetMidY(self.centerAnnotationView.bounds) + PIN_HEIGHT_OFFSET;
self.centerAnnotationView.center = CGPointMake(mapViewPoint.x + xoffset,
mapViewPoint.y + yoffset);
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
self.centerAnnotaion.coordinate = mapView.centerCoordinate;
[self moveMapAnnotationToCoordinate:mapView.centerCoordinate];
}
#end
Really the only interesting thing to not is that you have to offset the MKPinAnnotationView by a certain amount to account for the distance between the lower left corner and the pin head. I don't like having these asset dependent constants in the code, so if anyone can find a better way to do that, I am all ears.
I created a github project with a map controller that does this as well as some other things related to using a mapview to have a user select a location. Check it out here: https://github.com/scottrhoyt/CenterPinMapViewController
Swift version
In class:
var centerAnnotation = MKPointAnnotation()
var centerAnnotationView = MKPinAnnotationView()
In viewDidLoad:
if #available(iOS 9.0, *) {
centerAnnotationView.pinTintColor = customColor
} else {
// Fallback on earlier versions
centerAnnotationView.pinColor = MKPinAnnotationColor.Red
}
self.view.addSubview(centerAnnotationView)
In viewDidAppear:
self.moveMapAnnotationToCoordinate(self.mapView.centerCoordinate)
Then:
func moveMapAnnotationToCoordinate(locate : CLLocationCoordinate2D ) {
let mapViewPoint : CGPoint = self.mapView.convertCoordinate(locate, toPointToView: self.mapView)
let pinWidth : CGFloat = 7.75
let pinHeight : CGFloat = 7
let xOffset : CGFloat = CGRectGetMidX(self.centerAnnotationView.bounds) - pinWidth
let yOffset : CGFloat = CGRectGetMidY(self.centerAnnotationView.bounds) - pinHeight
self.centerAnnotationView.center = CGPointMake(mapViewPoint.x - xOffset, mapViewPoint.y - yOffset)
}
For using change in location, add delegate CLLocationManagerDelegate and then:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
self.centerAnnotation.coordinate = self.mapView.centerCoordinate
self.moveMapAnnotationToCoordinate(self.mapView.centerCoordinate)
}

Display annotation in Map kit

I am working with Mapkit and I have to show annotations in the map but I'm not able to display the annotation. Here's my code:
#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 <MKAnnotation>) 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.
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.
To add annotation use : addAnnotation:
read about it here