Random MKPointAnnotation in map - ios

In my project I fill my map with these informations
for (int i = 0; i<arrayResults.count; i++){
object = [arrayResults objectAtIndex:i];
if ([[object iden]intValue] == 1) {
type = #"type_one";
CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:#"latitude"] floatValue], [[[object coord]objectForKey:#"longitude"] floatValue]);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = [NSString stringWithFormat:#"%#",[object nome_obj]];
annotationPoint.subtitle = [NSString stringWithFormat:#"%#",[object address]];
[map addAnnotation:annotationPoint];
}
else if ([[object iden]intValue] == 2) {
type = #"type_two";
CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:#"latitude"] floatValue], [[[object coord]objectForKey:#"longitude"] floatValue]);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = [NSString stringWithFormat:#"%#",[object nome_obj]];
annotationPoint.subtitle = [NSString stringWithFormat:#"%#",[object address]];
[map addAnnotation:annotationPoint];
}
else if ([[object iden]intValue] == 3) {
type = #"type_three";
CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:#"latitude"] floatValue], [[[object coord]objectForKey:#"longitude"] floatValue]);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = [NSString stringWithFormat:#"%#",[object nome_obj]];
annotationPoint.subtitle = [NSString stringWithFormat:#"%#",[object address]];
[map addAnnotation:annotationPoint];
}
else if ([[object iden]intValue] == 4) {
type = #"type_four";
CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:#"latitude"] floatValue], [[[object coord]objectForKey:#"longitude"] floatValue]);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = [NSString stringWithFormat:#"%#",[object nome_obj]];
annotationPoint.subtitle = [NSString stringWithFormat:#"%#",[object address]];
[map addAnnotation:annotationPoint];
}
these are four type of object that I add on my map
when I enter inside this method
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView *view = nil;
if (annotation != mapView.userLocation) {
if ([[[arrayResults objectAtIndex:countPoint]iden]intValue] == 1){
view = [mapView dequeueReusableAnnotationViewWithIdentifier:#"type_one"];
if (!view) {
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"type_one"];
view.canShowCallout = YES;
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[aButton addTarget:self action:#selector(open:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTag:countPoint+100];
view.rightCalloutAccessoryView = aButton;
view.image = [UIImage imageNamed:#"type_one.png"];
}
}
else if ([[[arrayResults objectAtIndex:countPoint]iden]intValue] == 2){
view = [mapView dequeueReusableAnnotationViewWithIdentifier:#"type_two"];
if (!view) {
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"type_two"];
view.canShowCallout = YES;
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[aButton addTarget:self action:#selector(open:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTag:countPoint+100];
view.rightCalloutAccessoryView = aButton;
view.image = [UIImage imageNamed:#"type_two.png"];
}
}
else if ([[[arrayResults objectAtIndex:countPoint]iden]intValue] == 3){
view = [mapView dequeueReusableAnnotationViewWithIdentifier:#"type_three"];
if (!view) {
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"type_three"];
view.canShowCallout = YES;
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[aButton addTarget:self action:#selector(open:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTag:countPoint+100];
view.rightCalloutAccessoryView = aButton;
view.image = [UIImage imageNamed:#"type_three.png"];
}
}
else if ([[[arrayResults objectAtIndex:countPoint]iden]intValue] == 4){
view = [mapView dequeueReusableAnnotationViewWithIdentifier:#"type_four"];
if (!view) {
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"type_four"];
view.canShowCallout = YES;
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[aButton addTarget:self action:#selector(openScheda:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTag:countPoint+100];
view.rightCalloutAccessoryView = aButton;
view.image = [UIImage imageNamed:#"type_four.png"];
}
}
countPoint++;
}
The problem is that type of marker don't correspond with array element... but I don't understand.
That is if a marker has image "type_one", in the pop up it show "title" of type_four...it don't correspond

Your annotation needs to "know" what type it is so you can create the right kind of view in viewForAnnotation. Do so by subclassing MKPointAnnotation, adding an NSInteger value to it to store the array index.
See MKPointAnnotation add extra property, but you'll add an NSInteger property called index.
When creating your annotations, set this property:
for (int i = 0; i<arrayResults.count; i++)
{
object = [arrayResults objectAtIndex:i];
CLLocationCoordinate2D annotationCoord = CLLocationCoordinate2DMake([[[object coord]objectForKey:#"latitude"] floatValue], [[[object coord]objectForKey:#"longitude"] floatValue]);
// MKMyPointAnnotation is your MKPointAnnotation subclass
MKMyPointAnnotation *annotationPoint = [[MKMyPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = [NSString stringWithFormat:#"%#",[object nome_obj]];
annotationPoint.subtitle = [NSString stringWithFormat:#"%#",[object address]];
annotationPoint.index = i; // store the index
[map addAnnotation:annotationPoint];
}
Then in your viewForAnnotation, cast the annotation to your subclass, then dereference the type:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
{
MKMyPointAnnotation *myAnnotation = (MKMyPointAnnotation *)annotation;
object = [arrayResults objectAtIndex:myAnnotation.index]; // <-- your index property
switch ([[object iden] intValue])
{
case 1: // type one
.
.
.
}
.
.
.
}

Related

Pin colors not showed as expected as a regular basis

Following the first part of the answer to my question Detecting selected annotation to change pin color, I have updated my code as follows:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
//7
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
//8
static NSString *identifier = #"myAnnotation";
MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView)
{
//9
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
{
myAnnotation *myAnn = (myAnnotation *)annotation;
NSLog(#"ANNOTATION GRUPO = %d",myAnn.grupo);
switch (myAnn.grupo)
{
case 1:
annotationView.pinColor = MKPinAnnotationColorRed;
break;
case 2:
annotationView.pinColor = MKPinAnnotationColorGreen;
break;
case 3:
annotationView.pinColor = MKPinAnnotationColorPurple;
break;
default:
annotationView.pinColor = MKPinAnnotationColorPurple;
break;
}
}
annotationView.animatesDrop = YES;
annotationView.canShowCallout = YES;
}else {
annotationView.annotation = annotation;
}
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
}
And the changeOpcionmethod as follows:
- (IBAction)changeOpcion:(id)sender{
if(miControl.selectedSegmentIndex == 0)
{
//[mapView_ clear];
[self removeAllPinsButUserLocation2];
NSLog(#"********0");
for ( int i=0;i<[categorias count];i++){
int grupo = [[[categorias objectAtIndex:i] objectForKey:#"procedencia"] integerValue];
if (grupo == 1){
double latitud = [[[categorias objectAtIndex:i] objectForKey:#"latitud"] doubleValue];
double longitud = [[[categorias objectAtIndex:i] objectForKey:#"longitud"]doubleValue];
CLLocationCoordinate2D lugar;
lugar.latitude = latitud;
lugar.longitude = longitud;
NSString *nombre = [[categorias objectAtIndex:i] objectForKey:#"titulo"];
NSString *direccion = [[categorias objectAtIndex:i] objectForKey:#"id"];
CLLocationCoordinate2D coordinate3;
coordinate3.latitude = latitud;
coordinate3.longitude = longitud;
myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre subtitle:direccion];
annotation3.grupo = 1;
[self.mapView addAnnotation:annotation3];
}
}
}
else if(miControl.selectedSegmentIndex == 1)
{
[self removeAllPinsButUserLocation2];
for ( int i=0;i<[categorias count];i++){
int grupo = [[[categorias objectAtIndex:i] objectForKey:#"procedencia"] integerValue];
if (grupo == 2){
double latitud = [[[categorias objectAtIndex:i] objectForKey:#"latitud"] doubleValue];
double longitud = [[[categorias objectAtIndex:i] objectForKey:#"longitud"]doubleValue];
CLLocationCoordinate2D lugar;
lugar.latitude = latitud;
lugar.longitude = longitud;
NSString *nombre = [[categorias objectAtIndex:i] objectForKey:#"titulo"];
NSString *direccion = [[categorias objectAtIndex:i] objectForKey:#"direccion"];
CLLocationCoordinate2D coordinate3;
coordinate3.latitude = latitud;
coordinate3.longitude = longitud;
myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre subtitle:direccion];
annotation3.grupo = 2;
[self.mapView addAnnotation:annotation3];
}
}
//action for the second button
}
else if(miControl.selectedSegmentIndex == 2)
{
[self removeAllPinsButUserLocation2];
for ( int i=0;i<[categorias count];i++){
int grupo = [[[categorias objectAtIndex:i] objectForKey:#"procedencia"] integerValue];
if (grupo == 3){
double latitud = [[[categorias objectAtIndex:i] objectForKey:#"latitud"] doubleValue];
double longitud = [[[categorias objectAtIndex:i] objectForKey:#"longitud"]doubleValue];
CLLocationCoordinate2D lugar;
lugar.latitude = latitud;
lugar.longitude = longitud;
NSString *nombre = [[categorias objectAtIndex:i] objectForKey:#"titulo"];
NSString *direccion = [[categorias objectAtIndex:i] objectForKey:#"direccion"];
CLLocationCoordinate2D coordinate3;
coordinate3.latitude = latitud;
coordinate3.longitude = longitud;
myAnnotation *annotation3 = [[myAnnotation alloc] initWithCoordinate:coordinate3 title:nombre subtitle:direccion];
annotation3.grupo = 3;
[self.mapView addAnnotation:annotation3];
}
}
}
}
When the user selects option #2 (Eventos) from the segmentedIndex control, the pins are purple, as expected. Then if the user selects option #1 (Cursos), the pins are green, as expected, and if the user selects option #0 (Ofertas), the pins are red, as expected, but after that, if the user selects whatever option, all pins are red. Why ???
It sounds like the pin views are being re-used from previous annotations and their pinColor is not being updated when they are re-used.
In the answer that you linked to, there is this statement:
Finally, in viewForAnnotation, right before returning the view, set the pinColor based on grupo:
The pinColor needs to be set whether you are creating a new view or if you are re-using a dequeued view. A dequeued view may have had its pinColor set for an annotation in a different group.
The code shown in the question is only setting pinColor when creating a new view.
That code needs to be moved out of that block and put right before the return annotationView; line:
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//the code to set the pinColor goes HERE...
myAnnotation *myAnn = (myAnnotation *)annotation;
NSLog(#"ANNOTATION GRUPO = %d",myAnn.grupo);
switch (myAnn.grupo)
{
case 1:
annotationView.pinColor = MKPinAnnotationColorRed;
break;
case 2:
annotationView.pinColor = MKPinAnnotationColorGreen;
break;
case 3:
annotationView.pinColor = MKPinAnnotationColorPurple;
break;
default:
annotationView.pinColor = MKPinAnnotationColorPurple;
break;
}
return annotationView;

Adding action to MKMapView annotation

I have a MKMapView with a bunch of annotations but I can't add an action to them. I was just creating an MKPointAnnotation, but I couldn't add an action to that. Whenever I click the annotation, nothing happens. Here is how I am setting the annotations -
CLLocationCoordinate2D monteVista;
monteVista.latitude = (double) 37.83029;
monteVista.longitude = (double) -121.98827;
MKPointAnnotation *monteVistaPoint = [[MKPointAnnotation alloc] init];
monteVistaPoint.coordinate = monteVista;
monteVistaPoint.title = #"Monte Vista";
CLLocationCoordinate2D sanRamonValley;
sanRamonValley.latitude = (double) 37.82609;
sanRamonValley.longitude = (double) -122.00603;
MKPointAnnotation *sanRamonValleyPoint = [[MKPointAnnotation alloc] init];
sanRamonValleyPoint.coordinate = sanRamonValley;
sanRamonValleyPoint.title = #"San Ramon Valley";
CLLocationCoordinate2D doughertyValley;
doughertyValley.latitude = (double) 37.76845;
doughertyValley.longitude = (double) -121.90342;
MKPointAnnotation *doughertyValleyPoint = [[MKPointAnnotation alloc] init];
doughertyValleyPoint.coordinate = doughertyValley;
doughertyValleyPoint.title = #"Dougherty Valley";
NSArray *points = [[NSArray alloc] initWithObjects: monteVistaPoint, sanRamonValleyPoint, doughertyValleyPoint, nil];
[self.schoolMap addAnnotations:points];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
NSLog(#"Pin Created");
return annotationView;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
MKPointAnnotation *testPoint = [[MKPointAnnotation alloc] init];
testPoint = view.annotation;
self.testText.text = testPoint.title;
NSLog(#"Selected");
}
You need to change viewForAnnotation: method in order customize and then just implement openDetail: action method
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation)
{
return nil;
}
static NSString *identifier = #"CustomAnnotation";
MKAnnotationView* annView = nil;
if ([annotation isKindOfClass:[CustomAnnotation class]]) {
annView = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annView == nil) {
annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annView.annotation = annotation;
}
UIImage *image = nil;
image = [UIImage imageNamed:#"pin2"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[annView addSubview:imageView];
[annView setFrame:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
// [annView setBackgroundColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:0.1]];
UIButton*accessory = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[accessory setTag:[(CustomAnnotation*)annotation tag]];
[accessory addTarget:self action:#selector(openDetail:) forControlEvents:UIControlEventTouchUpInside];
[accessory setFrame:CGRectMake(0, 0, 30, 30)];
[annView setRightCalloutAccessoryView:accessory];
}
[annView setEnabled:YES];
[annView setCanShowCallout:YES];
return annView;
}

How to add an UIButton to placemark? ios objective c

How do I add a button to my placemarks and then get it to push onto a view controller?
- (void)viewDidLoad
{
[super viewDidLoad];
// Set "More" logo in navigation bar
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Navigation"]];
appDelegate=[[UIApplication sharedApplication] delegate];
// Set longatude and latitude to tyne and wear
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(54.995184, -1.566699);
// Set span to cover area
MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);
// Set region
MKCoordinateRegion regionToDisplay = MKCoordinateRegionMake(center, span);
[self.nearbyMapView setRegion: regionToDisplay];
for (int i = 0; i < [[appDelegate offersFeeds] count]; i++)
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
NSString *plotAddress = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:#"addressline"];
NSString *plotTitle = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:#"title"];
[geocoder geocodeAddressString:plotAddress completionHandler:^(NSArray *placemarks, NSError *error) {
if (placemarks && placemarks.count > 0)
{
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc]initWithPlacemark:topResult];
// Set title
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = placemark.location.coordinate;
pa.title = plotTitle;
// Add placemark to map
[self.nearbyMapView addAnnotation:pa];
}
}];
}
}
I've had a look at MKAnotationView but struggling to understand how to get this working with CLPlacemark.
Add this before you add mapview to self.view
-(void)viewDidLoad
{
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = yourCoordinate;
annotation.title = #"Title";
annotation.subtitle = #"SubTitle";
[mapView1 addAnnotation:annotation];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
static NSString *defaultPinID = #"identifier";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
{
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
annotationView.enabled = YES;
pinView.canShowCallout = YES;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//Accessoryview for the annotation view in ios.
pinView.rightCalloutAccessoryView = btn;
}
else
{
pinView.annotation = annotation;
}
pinView.annotation = annotation;
return pinView;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
//Put your button stuff here...
}
I hope it is useful to you

MapView add an action to custom annotation (the right arrow)

I create the annotation pins via this for loop:
UIButton *eventMore = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
for (i = 0; i < [results count]; i++) {
//NSLog(#"Result: %i = %#", i, results[i]);
//NSLog(#"%#",[[results objectAtIndex:i] objectForKey:#"long"]);
myAnn = [[CustomAnnotation alloc] init];
location.latitude = (double)[[[results objectAtIndex:i] objectForKey:#"lat"] doubleValue];
location.longitude = (double)[[[results objectAtIndex:i] objectForKey:#"long"] doubleValue];
myAnn.coordinate = location;
myAnn.title = [[results objectAtIndex:i] objectForKey:#"title"];
myAnn.subtitle = [[results objectAtIndex:i] objectForKey:#"strap"];
[eventMore addTarget:myAnn action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[locations addObject:myAnn];
//NSLog(#"%i", [[results objectAtIndex:i] objectForKey:#"lat"]);
}
What I am trying to do with this is add the detail disclosure button to the right of the title/description on the annotation however this does not add one in, and all the tuts I have found for this do it using this method - but it isnt working, any advice?
And the code for the action:
-(void)btnClicked:(id)sender{
UIAlertView *btnAlert = [[UIAlertView alloc] initWithTitle:#"title" message:#"msg" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:nil, nil];
[btnAlert show];
}
You are not setting the CalloutAccessoryView for your annotation.
Sample code
for (i = 0; i < [results count]; i++) {
MKPointAnnotation *myAnn = [[MKPointAnnotation alloc] init];
location.latitude = (double)[[[results objectAtIndex:i] objectForKey:#"lat"] doubleValue];
location.longitude = (double)[[[results objectAtIndex:i] objectForKey:#"long"] doubleValue];
myAnn.coordinate = location;
myAnn.title = [[results objectAtIndex:i] objectForKey:#"title"];
myAnn.subtitle = [[results objectAtIndex:i] objectForKey:#"strap"];
[locations addObject:myAnn];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *s = #"ann";
MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:s];
if (!pin) {
pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:s];
pin.canShowCallout = YES;
pin.image = [UIImage imageNamed:#"pin.png"];
pin.calloutOffset = CGPointMake(0, 0);
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button addTarget:self
action:#selector(viewDetails) forControlEvents:UIControlEventTouchUpInside];
pin.rightCalloutAccessoryView = button;
}
return pin;
}

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?

Resources