iOS中显示多个注释

iOS中显示多个注释

本文介绍了如何在MKMapView iOS中显示多个注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发的新手,我想在iOS的MKMapViewController中显示多个注释,因为我编写了一个代码,就像在viewDidLoad方法中一样

i am newbie in iOS Development i want to Show multiple Annotation in MKMapViewController in iOS for that i write a code as in my viewDidLoad method

- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate=self;
NSArray *name=[[NSArray alloc]initWithObjects:
               @"VelaCherry",
               @"Perungudi",
               @"Tharamani", nil];
self.annotation=[[NSMutableArray alloc]initWithCapacity:[name count]];
MKPointAnnotation *mappin=[[MKPointAnnotation alloc]init];

CLLocationCoordinate2D location;

location.latitude=(double)12.970760345459;
location.longitude=(double)80.2190093994141;
mappin.coordinate=location;
mappin.title=[name objectAtIndex:0];
[self.annotation addObject:mappin];

location.latitude=(double)12.9752297537231;
location.longitude=(double)80.2313079833984;
mappin.coordinate=location;
mappin.title=[name objectAtIndex:1];
[self.annotation addObject:mappin];

location.latitude=(double)12.9788103103638;
location.longitude=(double)80.2412414550781;
mappin.title=[name objectAtIndex:2];
[self.annotation addObject:mappin];

[self.mapView addAnnotations:self.annotation];
self.mapView.mapType = MKMapTypeStandard;
self.mapView.showsUserLocation = YES;
}

但是它没有在MKMapViewController中显示任何注释,请为此提供解决方案.

But it is not show any of annotation in MKMapViewController please give me Solution for this.

推荐答案

我在这里编写了一个演示应用程序,其中考虑到Paulw11的建议,向您展示了一种使代码更简洁和可重用的方法.

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.

#import <MapKit/MapKit.h>

@interface ViewController : UIViewController <MKMapViewDelegate>

@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:

这篇关于如何在MKMapView iOS中显示多个注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:37