Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All Annotation is not visible to user in MKMapView

I am using MKMapView and have added a number of annotation pins to the map. All annotation belong to different country, so i do not able to set all annotation visible to user.

Please do try using below latitude and longitude for annotation.

First annotation Latitude:23.029690

First annotation Longitude: 72.527359

Second annotation Latitude:34.210855

Second annotation Longitude: -118.622636

Code for add annotation to mapview.

CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([[dicObjects valueForKey:K_Latitude] doubleValue], [[dicObjects valueForKey:K_Longitude] doubleValue]);
MyCustomAnnotation *myCustomAnnotation = [[MyCustomAnnotation alloc]initWithTitle:[NSString stringWithFormat:@"%@           ",[dicObjects valueForKey:K_Title]] Subtitle:[NSString stringWithFormat:@"%@",[dicObjects valueForKey:K_SubTitle]] andDescritpion:[NSString stringWithFormat:@"%@",[dicObjects valueForKey:K_TitelDes]] withLocation:coordinate andImageName:[dicObjects valueForKey:K_Rating] andTagValue:intTempCounter];
[mapViewPledgeList addAnnotation:myCustomAnnotation];

MyCustomAnnotation.h

@interface MyCustomAnnotation : NSObject<MKAnnotation>

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *subTitle;
@property (copy, nonatomic) NSString *titleDescritpion;
@property (copy, nonatomic) NSString *strImageName;
@property  int intTagValue;


- (id)initWithTitle:(NSString *)newTitle Subtitle:(NSString *)subTitle andDescritpion:(NSString *)titleDescritpion withLocation:(CLLocationCoordinate2D)location andTagValue:(int) intTag;
- (MKAnnotationView *)annotationView;
- (id)initWithTitle:(NSString *)newTitle Subtitle:(NSString *)subTitle andDescritpion:(NSString *)titleDescritpion withLocation:(CLLocationCoordinate2D)location andImageName:(NSString*) strImageName andTagValue:(int) intTag;
@end

MyCustomAnnotation.m

#import "MyCustomAnnotation.h"

@implementation MyCustomAnnotation


#pragma mark - Custom Annotation View Setup
- (MKAnnotationView *)annotationView{

    MKAnnotationView *annotationView = [[MKAnnotationView alloc]initWithAnnotation:self reuseIdentifier:K_ID_CUSTOM_ANNOTATION];
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.image = kImage(_strImageName);

    UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    button.frame = CGRectMake(0, 0, 23, 23);
    button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [button setImage:[[UIImage imageNamed:@"NextArrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    annotationView.rightCalloutAccessoryView = button;

    return annotationView;
}

- (id)initWithTitle:(NSString *)newTitle Subtitle:(NSString *)subTitle andDescritpion:(NSString *)titleDescritpion withLocation:(CLLocationCoordinate2D)location andImageName:(NSString*) strImageName andTagValue:(int) intTag{

    self = [super init];

    if(self){
        _title = newTitle;
        _subTitle = subTitle;
        _titleDescritpion = titleDescritpion;
        _coordinate = location;


        if ([strImageName intValue] == 1) {
            _strImageName = @"MapViewPinRed";

        }else if([strImageName intValue] == 2){
            _strImageName = @"MapViewPinOrange";

        }else if ([strImageName intValue] == 3){
            _strImageName = @"MapViewPinYellow";

        }else if ([strImageName intValue] == 4){
            _strImageName = @"MapViewPinLightGreen";

        }else{
            _strImageName = @"MapViewPinGreen";

        }
    }

    return self;
}

    @end

I was try below solution but it is not working.

1) Using MKMapRectUnion

 MKMapRect zoomRect = MKMapRectNull;
    for (id <MKAnnotation> annotation in mapViewPledgeList.annotations)
    {
        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
        zoomRect = MKMapRectUnion(zoomRect, pointRect);
    }
    [mapViewPledgeList setVisibleMapRect:zoomRect animated:YES];

2) Using showAnnotations

 [mapViewPledgeList showAnnotations:mapViewPledgeList.annotations animated:YES];

Issue:- suggest any solution so all annotation keep visible to user.

like image 277
Hitesh Surani Avatar asked Oct 18 '22 12:10

Hitesh Surani


1 Answers

Don't forget to add annotation to the map. Maybe this could be the problem

[self.mapView addAnnotation:point]; 
like image 142
Emre YILMAZ Avatar answered Nov 11 '22 08:11

Emre YILMAZ