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:
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
- I think you should handle your data, separated with your view. i.e. implement
geocoder
related code in themapView: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 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
Swiftui Navigationlink Loads Destination View Immediately, Without Clicking
How to Create a Swift Date Object
How Would I Tint an Image Programmatically on Ios
How to Animate the Textcolor Property of an Uilabel
Issue Using Cccrypt (Commoncrypt) in Swift
Importing Project-Swift.H into a Objective-C Class...File Not Found
Wkwebview Equivalent for Uiwebview's Scalespagetofit
Best Way to Check If Uitableviewcell Is Completely Visible
How to Make a Push Segue When a Uitableviewcell Is Selected
Changing Specific Text's Color Using Nsmutableattributedstring in Swift
How to Use Uiview Autoresizingmask Property Programmatically
Draw Segments from a Circle or Donut
iOS Detect If User Is on an iPad
How to Use Protocol as Associatedtype in Another Protocol in Swift
How to Tell Swiftui Views to Bind to Nested Observableobjects