Positioning Mkmapview to Show Multiple Annotations at Once

Positioning MKMapView to show multiple annotations at once

As of iOS7 you can use showAnnotations:animated:

[mapView showAnnotations:annotations animated:YES];

How to Show Multiple Annotation in MKMapView iOS?

I've written a demo app here which shows you one way to make your code a bit more cleaner and reusable, taking into account Paulw11's sugggestion.

Note, this method is purely done with code, no interface builder.

ViewController.h

#import 

@interface ViewController : UIViewController

@property (nonatomic, strong) MKMapView *mapView;

@end

ViewController.m

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self initViews];
[self initConstraints];

[self addAllPins];
}

-(void)initViews
{
self.mapView = [[MKMapView alloc] init];
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;

MKCoordinateRegion region = self.mapView.region;

region.center = CLLocationCoordinate2DMake(12.9752297537231, 80.2313079833984);

region.span.longitudeDelta /= 1.0; // Bigger the value, closer the map view
region.span.latitudeDelta /= 1.0;
[self.mapView setRegion:region animated:NO]; // Choose if you want animate or not

[self.view addSubview:self.mapView];
}

-(void)initConstraints
{
self.mapView.translatesAutoresizingMaskIntoConstraints = NO;

id views = @{
@"mapView": self.mapView
};

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mapView]|" options:0 metrics:nil views:views]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[mapView]|" options:0 metrics:nil views:views]];
}

-(void)addAllPins
{
self.mapView.delegate=self;

NSArray *name=[[NSArray alloc]initWithObjects:
@"VelaCherry",
@"Perungudi",
@"Tharamani", nil];

NSMutableArray *arrCoordinateStr = [[NSMutableArray alloc] initWithCapacity:name.count];

[arrCoordinateStr addObject:@"12.970760345459, 80.2190093994141"];
[arrCoordinateStr addObject:@"12.9752297537231, 80.2313079833984"];
[arrCoordinateStr addObject:@"12.9788103103638, 80.2412414550781"];

for(int i = 0; i < name.count; i++)
{
[self addPinWithTitle:name[i] AndCoordinate:arrCoordinateStr[i]];
}
}

-(void)addPinWithTitle:(NSString *)title AndCoordinate:(NSString *)strCoordinate
{
MKPointAnnotation *mapPin = [[MKPointAnnotation alloc] init];

// clear out any white space
strCoordinate = [strCoordinate stringByReplacingOccurrencesOfString:@" " withString:@""];

// convert string into actual latitude and longitude values
NSArray *components = [strCoordinate componentsSeparatedByString:@","];

double latitude = [components[0] doubleValue];
double longitude = [components[1] doubleValue];

// setup the map pin with all data and add to map view
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);

mapPin.title = title;
mapPin.coordinate = coordinate;

[self.mapView addAnnotation:mapPin];
}

If you zoom out a little, you'll see all three pins:

screenshot of demo app

Positioning GMSMapView to show multiple annotations at once

Thats very simple, just:

@property (nonatomic, strong) GMSMapView *mapView;

- (void)didTapFitBoundsWithMarkers:(NSArray *)markers
{
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];;

for (GMSMarker *marker in markers) {
bounds = [bounds includingCoordinate:marker.position];
}

GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds];
[self.mapView moveCamera:update];
[self.mapView animateToViewingAngle:50];
}

Adding Multiple Annotations to Map But All Show Some of Same Data

Subclass MKPointAnnotation for example MyPointAnnotation add a property result

@interface MyPointAnnotation : MKPointAnnotation
@property (nonatomic, strong) NSString *result;
@end

In viewWillAppear: modify these lines

NSString *theResult = obj[@"Result"];

MyPointAnnotation *myAnnotation = [[MyPointAnnotation alloc] init];
[myAnnotation setCoordinate:coord2];
[myAnnotation setTitle:theTitle];
[myAnnotation setSubtitle:combined];
[myAnnotation setResult:theResult];

[mapView addAnnotation:myAnnotation];

In viewForAnnotation: delegate method get the result like this

MyPointAnnotation *myAnnotation = (MyPointAnnotation *)annotation;
NSString *theResult = myAnnotation.result;

Show multiple map annotations/coordinates on mapview

You are accessing only one object ?

NSString *location = self.addressData[0][@"address"];

Edited

  1. I think you should handle your data, separated with your view. i.e. implement geocoder related code in the mapView:viewForAnnotation: method in your map view delegate. Then you should be able to create the annotations one by one and use [self.mapView addAnnotations] for all of them
  2. For your code, which I believe is inspired by this answer, you should be able to iterate through all location addresses by something like

    for (NSMutableDictionary *loc in self.addressData) {
    NSString *loc = location[@"address"];

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    ......
    }

    Forgive me if the syntax is wrong for Objective C.

Is it possible to select multiple MKAnnotationViews?

I discovered that I had been looking at this problem for too long and was making it much harder than it was. Using MapKit, it is not possible to handle multiple selected annotations at one time.

Instead, I had to override the MKMapViewDelegate methods mapView:didSelectAnnotationView: and mapView:didDeselectAnnotationView: and create my own methods to handle saving the selected annotations to an array and removing them as well. Those methods also handled setting the appearance of selected annotations.



Related Topics



Leave a reply



Submit