iOS Displaying annotations from an array - ios

I am trying to display multiple annotations on my mapView from an array. The annotation coordinates are parsed from an XML file and stored in the array as [currentCall longitude] and [currentCall latitude]. My question is, what is the syntax for "calling" the array? (I don't know if that's how you say it). In another part of my application, I display the parsed XML results in a table and use "JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];" to "call" the array. How do I do it for displaying my annotations? Everything else works fine except that little part.
Here is the implementation file:
#implementation SecondViewController
#synthesize mapView;
XMLParser *xmlParser;
-(IBAction)getlocation {
mapView.showsUserLocation = YES;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView commitAnimations];
}
-(IBAction)changeSeg:(id)sender {
if (segment.selectedSegmentIndex == 0) {
mapView.mapType = MKMapTypeStandard;
}
if (segment.selectedSegmentIndex == 1) {
mapView.mapType = MKMapTypeSatellite;
}
if (segment.selectedSegmentIndex == 2) {
mapView.mapType = MKMapTypeHybrid;
}
}
-(void)viewDidLoad {
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
[self.view insertSubview:mapView atIndex:0];
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setDelegate:self];
MKCoordinateRegion WCCCA = { {0.0, 0.0} , {0.0, 0.0} };
WCCCA.center.latitude = 45.53540820864449;
WCCCA.center.longitude = -122.86178648471832;
WCCCA.span.longitudeDelta = 0.02f;
WCCCA.span.latitudeDelta = 0.02f;
[mapView setRegion:WCCCA animated:YES];
Annotation *ann1 = [[Annotation alloc] init];
ann1.title = #"WCCCA";
ann1.subtitle = #"Washington County Consolidated Communications Agency";
ann1.coordinate = WCCCA.center;
[mapView addAnnotation: ann1];
MKCoordinateRegion CALL = { {0.0, 0.0} , {0.0, 0.0} };
CALL.center.latitude = [currentCall.latitude doubleValue];
CALL.center.longitude = [currentCall.longitude doubleValue];
CALL.span.longitudeDelta = 0.02f;
CALL.span.latitudeDelta = 0.02f;
[mapView setRegion:WCCCA animated:YES];
Annotation *ann2 = [[Annotation alloc] init];
ann2.title = [currentCall currentCallType];
ann2.subtitle = [currentCall location];
ann2.coordinate = CALL.center;
[mapView addAnnotation: ann1];
}
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
MyPin.pinColor = MKPinAnnotationColorRed;
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop=TRUE;
MyPin.canShowCallout = YES;
return MyPin;
}
#end

If I am understanding your code properly, you should be able to just iterate through your parsed XML (it appears that the output the from your xmlParser object is an NSArray) and add the annotations to the mapView within the loop.
You can use the C function CLLocationCoordinate2DMake() to create the coordinate data structure for your annotation.
NSArray *callsArray = [xmlParser calls];
for (JointCAD *call in callsArray) {
Annotation *ann = [[Annotation alloc] init];
ann.title = [call currentCallType];
ann.subtitle = [call location];
ann.coordinate = CLLocationCoordinate2DMake([call latitude], [call longitude]);
[mapView addAnnotation:ann];
}

Related

How to detect another MKAnnotaion pin when touch

I have multiple annotation pin in map(place1,place2)
I want user touch pin to go to new Viewcontroller to get description of place
it have function to detect in pin to touch it to go to descriptionViewController?
This is my code
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MKCoordinateRegion place1 = { {0.0, 0.0} , {0.0, 0.0} };
place1.center.latitude = 34.7037755;
place1.center.longitude = 137.7345882;
place1.span.longitudeDelta = 0.02f;
place1.span.latitudeDelta = 0.02f;
[mapView setRegion:place1 animated:YES];
Newclass *ann1 = [[Newclass alloc] init];
ann1.title = #"Place1";
ann1.subtitle = #"subtitle";
ann1.coordinate = place1.center;
[mapView addAnnotation: ann1];
MKCoordinateRegion place2 = { {0.0, 0.0} , {0.0, 0.0} };
place2.center.latitude = 34.7024461;
place2.center.longitude = 137.7297572;
place2.span.longitudeDelta = 0.02f;
place2.span.latitudeDelta = 0.02f;
[mapView setRegion:place2 animated:YES];
Newclass *ann2 = [[Newclass alloc] init];
ann2.title = #"place2";
ann2.subtitle = #"subtitle";
ann2.coordinate = place2.center;
[mapView addAnnotation:ann2];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *myPin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"current"];
myPin.pinColor = MKPinAnnotationColorGreen;
UIButton *advertButtom = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButtom addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
myPin.rightCalloutAccessoryView = advertButtom;
myPin.draggable =NO;
myPin.animatesDrop =true;
myPin.canShowCallout=YES;
return myPin;
}
-(void)button:(id)sender{
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
UIStoryboard *storyboard = [self storyboard];
descriptionViewController *description = [storyboard instantiateViewControllerWithIdentifier:#"des"];
[self presentModalViewController:description animated:YES];
NSLog(#"touched");
NSLog(#"data %#");
}
You need to use mapView method
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
UIStoryboard *storyboard = [self storyboard];
descriptionViewController *description = [storyboard instantiateViewControllerWithIdentifier:#"des"];
[self presentModalViewController:description animated:YES];
NSLog(#"touched");
NSLog(#"data %#");
}

Annotated pins are not getting loaded during reloading of map

I have used apple maps in my application for implementing maps.I used the following code for that.
My problem is that,It is showing the pins only when it is loading for the first time.
I have written the code like this
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Locate Engineer";
[worldView setMapType:MKMapTypeStandard];
[worldView setShowsUserLocation:YES];
worldView.delegate = self;
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self)
{
i=0;
locationManager = [[CLLocationManager alloc]init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
}
return self;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"%#",newLocation);
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
pinView = nil;
if(annotation != mapView.userLocation)
{
NSString * defaultPinID = #"aa";
//removed autorelease from the code below by coder
pinView = (MKPinAnnotationView *) [worldView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if(pinView == nil)
{
pinView .tag =i;
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] ;
//pinView.image = [UIImage imageNamed:#"serviceengineer.png"];
NSLog(#"pnID is %d",i);
NSLog(#"value of i is %d",i);
if ([[pinView.annotation title] isEqualToString:#"Levitton"]) {
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.draggable = NO;
} else {
pinView.pinColor = MKPinAnnotationColorGreen;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(mapAction:) forControlEvents:UIControlEventTouchUpInside];
rightButton.tag =i;
pinView.rightCalloutAccessoryView = rightButton;
// pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.draggable = NO;
}
}
else {
[worldView.userLocation setTitle:#"Title here"]; }
i++;
}
return pinView;
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
ServiceEngineersList *engList = [ServiceEngineers findAll];
MKCoordinateSpan spans;
spans.latitudeDelta = 0.5;
spans.longitudeDelta = 0.5;
ServiceEngineers *obj1= [engList objectAtIndex:0];
// location1.latitude = 40.728224000000;
//location1.longitude =-73.794852000000;
location1.latitude = [obj1.LATITUDE doubleValue];
location1.longitude =[obj1.LONGITUDE doubleValue];
ad1 = [[annotationTest alloc] init];
ad1.pinColor = MKPinAnnotationColorGreen;
MKCoordinateRegion regions =
MKCoordinateRegionMakeWithDistance(location1, 250,250);
regions.span = spans;
[worldView setRegion:regions animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad1];
ServiceEngineers *obj2= [engList objectAtIndex:1];
ad2 = [[annotationTest2 alloc] init];
ad2.pinColor = MKPinAnnotationColorGreen;
//location2.latitude = 40.710841000000;
//location2.longitude=-73.897769000000;
location2.latitude = [obj2.LATITUDE doubleValue];
location2.longitude =[obj2.LONGITUDE doubleValue];
MKCoordinateRegion region1 =
MKCoordinateRegionMakeWithDistance(location2, 250, 250);
region1.span = spans;
[worldView setRegion:region1 animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad2];
ServiceEngineers *obj3= [engList objectAtIndex:2];
ad3 = [[annotationTest3 alloc] init];
ad3.pinColor = MKPinAnnotationColorGreen;
//location3.latitude = 40.726768000000;
//location3.longitude=-73.634295000000;
location3.latitude = [obj3.LATITUDE doubleValue];
location3.longitude =[obj3.LONGITUDE doubleValue];
MKCoordinateRegion region2 =
MKCoordinateRegionMakeWithDistance(location3, 250, 250);
region2.span = spans;
[worldView setRegion:region2 animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad3];
ServiceEngineers *obj4= [engList objectAtIndex:3];
ad4 = [[annotationTest4 alloc] init];
ad4.pinColor = MKPinAnnotationColorGreen;
//location4.latitude = 40.702677000000;
//location4.longitude=-73.788969000000;
location4.latitude = [obj4.LATITUDE doubleValue];
location4.longitude =[obj4.LONGITUDE doubleValue];
MKCoordinateRegion region3 =
MKCoordinateRegionMakeWithDistance(location4, 250, 250);
region3.span = spans;
[worldView setRegion:region3 animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad4];
ad5 = [[annotationTest5 alloc] init];
ad5.pinColor = MKPinAnnotationColorRed;
location5.latitude = 40.7258;
location5.longitude= -73.5147;
MKCoordinateRegion region4 =
MKCoordinateRegionMakeWithDistance(location5, 250, 250);
region4.span = spans;
[worldView setRegion:region4 animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad5];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"Couldnot find location: %#",error);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
return YES;
return NO;
}
Please help me to find out the issue.I put breakpoints.Second time it is coming only upto
worldView.delegate = self;
Just place in the viewWillAppear and remove in the viewDidLoad
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
locationManager = [[CLLocationManager alloc]init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
}

TableView & MapView same Detail View

I have a table view that load the data from an xml in a array and show it, every cell point to the same detail view repopulated.
In the same view i have a hidden map view, the map view show the same array of the table in the map, every annotation have a disclosure button that point to the same detail view of the table, but i don't understand how i can pass the data from the map view to the detail view...
for the disclosure button i use this code
-(IBAction)showDetails:(id)sender{
DettMercatiViewController *dettMercatiViewController = [[DettMercatiViewController alloc] initWithNibName:#"DettMercatiViewController" bundle:nil];
dettMercatiViewController.mieiMercati = [table objectAtIndex:????];
[self.navigationController pushViewController:dettMercatiViewController animated:YES];
[dettMercatiViewController release];
}
table is the name of the nsmutablearray created from the xml but i don't know what objectIndex use... any idea?
ok, here the viewdidload and the creation of pin, please help me and explain because i am desolate:
- (void)viewDidLoad
{
pp =0;
//Colore NavigationBar
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:33.0f/255.0f green:89.0f/255.0f blue:50.0f/255.0f alpha:1.0f];
//Font Navigation Bar
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:#"Museo700-Regular" size:20];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=[self.title uppercaseString];
self.navigationItem.titleView = label;
[label release];
//creazione nome XML
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *nomeXML = [NSString stringWithFormat: #"%#_%#_%#",[defaults stringForKey:#"interesse"],[defaults stringForKey:#"provincia"],[defaults stringForKey:#"giorno"]];
NSLog(#"%#", nomeXML);
//Caricamento XML
table = [[NSMutableArray alloc] init];
NSString *url = [[NSBundle mainBundle] pathForResource:nomeXML ofType:#"xml"];
XMLParser *myParser = [[[XMLParser alloc]autorelease] parseXMLAtURL:url toObject:#"Mercato" parseError:nil];
for(int i = 0; i < [[myParser items] count]; i++) {
Mercato *new = [[myParser items] objectAtIndex:i];
[table addObject:new];
///////////////////////////////////////////
//definizione posizione utente
AppDelegate *appDelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
CLLocation *userLoc = appDelegate.locationManager.location;
CLLocationCoordinate2D userCoordinate = userLoc.coordinate;
NSLog(#"user latitude = %f",userCoordinate.latitude);
NSLog(#"user longitude = %f",userCoordinate.longitude);
mapView.delegate=self;
//Pin Mappa
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = [new.latitudine doubleValue];
theCoordinate.longitude = [new.longitudine doubleValue];
myAnnotation=[[MyAnnotation alloc] init];
myAnnotation.coordinate=theCoordinate;
myAnnotation.title=[NSString stringWithFormat:#"%#", new.ubicazione];
myAnnotation.subtitle=[NSString stringWithFormat:#"%#, %#, %#",new.comune, new.giorno, new. orario];
[mapView addAnnotation:myAnnotation];
[annotations addObject:myAnnotation];
if([new.latitudine doubleValue] == 0) {
[mapView removeAnnotation:myAnnotation];
myAnnotation = nil;
}
// Inizializza mappa al Centro della Provincia
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"bergamo"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.6982642;
newRegion.center.longitude = 9.6772698;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"brescia"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.5411875;
newRegion.center.longitude = 10.2194437;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"como"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.8080597;
newRegion.center.longitude = 9.0851765;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"cremona"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.133249;
newRegion.center.longitude = 10.0226511;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"lecco"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.8565698;
newRegion.center.longitude = 9.3976704;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"lodi"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.3138041;
newRegion.center.longitude = 9.5018274;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"mantova"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.1564168;
newRegion.center.longitude = 10.7913751;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"milano"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.4654542;
newRegion.center.longitude = 9.186516;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.512872;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"monza e brianza"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.623599;
newRegion.center.longitude = 9.2588015;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"pavia"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.1847248;
newRegion.center.longitude = 9.1582069;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"sondrio"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 46.1698583;
newRegion.center.longitude = 9.8787674;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
if (([[defaults stringForKey:#"provincia"] isEqualToString:#"varese"])) {
MKCoordinateRegion newRegion;
newRegion.center.latitude = 45.8205989;
newRegion.center.longitude = 8.8250576;
newRegion.span.latitudeDelta = 0.512872;
newRegion.span.longitudeDelta = 0.509863;
[mapView setRegion:newRegion animated:YES];
}
}
////////////////////////////
[super viewDidLoad];
}
#pragma mark MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
NSLog(#"welcome into the map view annotation");
pp++;
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]]){
return nil;
}
// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:AnnotationIdentifier]autorelease];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
pinView.pinColor=MKPinAnnotationColorGreen;
pinView.tag = pp;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:myAnnotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
return pinView;
}
////////////////
-(IBAction)showDetails:(id)sender{
NSLog(#"%i", pp);
DettMercatiViewController *dettMercatiViewController = [[DettMercatiViewController alloc] initWithNibName:#"DettMercatiViewController" bundle:nil];
dettMercatiViewController.mieiMercati = [table objectAtIndex:[sender tag]];
[self.navigationController pushViewController:dettMercatiViewController animated:YES];
[dettMercatiViewController release];
}
I suggest to give every disclosure button in your map view a tag and you can use this tag than
so
[table objectAtIndex:[sender tag]];

Tapping on MKAnnotationView doesn't call didSelectAnnotationView delegate

Take a look at this code and see if you can help me fixing it:
- (void)viewDidLoad
{
//Initialize the array and add annotations for the location on the map
annotations = [[NSMutableArray alloc] init];
CustomAnnotation *annotation;
CLLocationCoordinate2D coordinate;
//1
coordinate.latitude = 25.220390105797264;
coordinate.longitude = 55.28095199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"123";
[annotations addObject:annotation];
//2
coordinate.latitude = 25.218880105797264;
coordinate.longitude = 55.27992199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"117";
[annotations addObject:annotation];
//3
coordinate.latitude = 25.219280105797264;
coordinate.longitude = 55.28052199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"118";
[annotations addObject:annotation];
//4
coordinate.latitude = 25.219680105797264;
coordinate.longitude = 55.28052199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"119";
[annotations addObject:annotation];
//5
coordinate.latitude = 25.219980105797264;
coordinate.longitude = 55.28095199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"120";
[annotations addObject:annotation];
//6
coordinate.latitude = 25.220380105797264;
coordinate.longitude = 55.28115199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"121";
[annotations addObject:annotation];
//7
coordinate.latitude = 25.220350105797264;
coordinate.longitude = 55.28105199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"122";
[annotations addObject:annotation];
//8
coordinate.latitude = 25.215050105797264;
coordinate.longitude = 55.28105199798584;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"124";
[annotations addObject:annotation];
//9
coordinate.latitude = -35.44138;
coordinate.longitude = -71.66208;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"125";
[annotations addObject:annotation];
//10
coordinate.latitude = -35.44138;
coordinate.longitude = -71.66208;
annotation = [[CustomAnnotation alloc] initWithCoordinate:coordinate];
annotation.annotationIndex = #"126";
[annotations addObject:annotation];
//Set the region and zoom factor of the map view
MKCoordinateRegion region;
region.span.latitudeDelta = 0.01f;
region.span.longitudeDelta = 0.01f;
region.center.latitude = 25.216050105797264;
region.center.longitude = 55.27905199798584;
[sheikhZayedRoadMapView addAnnotations:annotations];
[sheikhZayedRoadMapView setRegion:region];
//[siteMapView.layer setCornerRadius:5.0];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:
(id <MKAnnotation>)annotation
{
NSLog(#"Annotation Class: %#", [annotation class]);
static NSString * const kPinAnnotationIdentifier = #"PinIdentifier";
MKAnnotationView *annotationView;
Myriadproregular *annotationIndexLabel;
//CustomAnnotation *customAnnotation =nil
annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:kPinAnnotationIdentifier];
if (!annotationView) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kPinAnnotationIdentifier];
annotationIndexLabel = [[Myriadproregular alloc] initWithFrame:CGRectMake(0, 3, 25 , 20)];
annotationIndexLabel.font = [UIFont boldSystemFontOfSize:12];
[annotationIndexLabel setBackgroundColor:[UIColor clearColor]];
[annotationIndexLabel setTextColor:[UIColor whiteColor]];
[annotationIndexLabel setTextAlignment:UITextAlignmentCenter];
[annotationView addSubview:annotationIndexLabel];
annotationView.canShowCallout = YES;
annotationView.calloutOffset = CGPointMake(-5, 5);
}else {
//Get the label if exists
for (UIView *labelView in annotationView.subviews) {
if ([labelView isKindOfClass:[UILabel class]]) {
annotationIndexLabel = (Myriadproregular *) labelView;
break;
}
}
}
if ([annotation isKindOfClass:[MKUserLocation class]]) {
[annotationView setImage:[UIImage imageNamed:#"map_notify_bg.png"]];
}else {
CustomAnnotation *customAnnotation = (CustomAnnotation *) annotation;
[annotationIndexLabel setText:customAnnotation.annotationIndex];
[annotationView setImage:[UIImage imageNamed:#"ico_inspector.png"]];
}
return annotationView;
}
Problem is:
I am not getting didSelectAnnotationView delegate method called, when I tap on the custom image provided to MKAnnotationView.
Any kind of help will be appreciated.
Thanks
[annotationView setCanShowCallout:NO]
This solved my problem, because I didn't want a callout on click of a pin, but I wanted to call the didSelectAnnotationView delegate to be called.
Thanks
Are you giving your annotations a title property? Are you sure that [annotationView setCanShowCallout:YES] is set properly?

Two Annotation Pins at a time in MKMapView

I successfully implemented MKMapVIew and a single annotation on my Map. I am not able to represent two postitions simultaneously. I am using MKMapViewDelegate method :
mapView:viewForAnnotation:
Can someone look into this thing.
Thanks!
EDIT
- (void)viewDidLoad
{
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 22.569722 ;
region.center.longitude = 88.369722;
region.span.longitudeDelta = 0.1f;
region.span.latitudeDelta = 0.1f;
MKCoordinateRegion anotherRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };
anotherRegion.center.latitude = 28.38 ;
anotherRegion.center.longitude = 77.12;
anotherRegion.span.longitudeDelta = 90.0f;
anotherRegion.span.latitudeDelta = 90.0f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = #" Kolkata";
ann.subtitle = #"Mahatma Gandhi Road";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
DisplayAnotherMap *annMap = [[DisplayAnotherMap alloc] init];
annMap.title = #" New Delhi";
annMap.subtitle = #"Shahdara";
annMap.coordinate = anotherRegion.center;
[mapView addAnnotations:[NSArray arrayWithObjects:annMap,ann,nil]];
}
This will fulfill the requirement for you! ...:)
The method mapView:viewForAnnotation: is just for the view of a the annotations, eg. the colour of the pin and the title label etc. If you want to show multiple annotations you should alloc and init the all with the required positions. For example, you could create a .plist with all the coordinates and the in for cycle just add them.
EDIT
This is a sample code, taken from somewhere. You must alloc and init all anotations.
-(void)loadDummyPlaces{
srand((unsigned)time(0));
NSMutableArray *tempPlaces=[[NSMutableArray alloc] initWithCapacity:0];
for (int i=0; i<1000; i++) {
MyPlace *place=[[MyPlace alloc] initWithCoordinate:CLLocationCoordinate2DMake([self RandomFloatStart:42.0 end:47.0],[self RandomFloatStart:14.0 end:19.0])];
[place setCurrentTitle:[NSString stringWithFormat:#"Place %d title",i]];
[place setCurrentSubTitle:[NSString stringWithFormat:#"Place %d subtitle",i]];
[place addPlace:place];
[tempPlaces addObject:place];
[place release];
}
places=[[NSArray alloc] initWithArray:tempPlaces];
[tempPlaces release];
}

Resources