如何在MKMapView iOS中显示多个注释?

阿什什·加巴尼(Ashish Gabani)

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

- (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中显示任何注释,请为此提供解决方案。

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

请注意,此方法完全由代码完成,没有接口生成器。

ViewController.h

#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];
}

如果缩小一点,您将看到所有三个销钉:

演示应用程序的屏幕截图

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Mkmapview中拖放注释图钉

来自分类Dev

如何在iOS MKMapView中设置标记

来自分类Dev

无法在MKMapView中显示多个注释图钉

来自分类Dev

如何在MKMapview上快速显示MKPinAnnotationView

来自分类Dev

如何在OS X中打印MKMapView?

来自分类Dev

如何在MKMapView上显示默认位置指示器

来自分类Dev

如何在iOS MKMapView上达到最大缩放级别

来自分类Dev

如何在MKMapView Swift中添加叠加路径

来自分类Dev

如何在MKMapView中的MKAnnotation上设置图像

来自分类Dev

如何在MKMapView中添加自定义透明MKAnnotation?

来自分类Dev

如何在mkmapview中查找用户的当前位置

来自分类Dev

如何在MKMapView中添加大半径标注?

来自分类Dev

如何在Adobe Acrobat Pro XI中添加引用多个突出显示文本的注释?

来自分类Dev

iOS如何在点击后使MKMapView在MKAnnotationView标注附件上居中?

来自分类Dev

iOS如何在点击后使MKMapView在MKAnnotationView标注附件上居中?

来自分类Dev

如何在Firebug中显示HTML注释?

来自分类Dev

使用自定义图块时如何在MKMapView中删除或隐藏背景

来自分类Dev

如何使用注释降低MKMapview的缩放级别?

来自分类Dev

如何使用注释降低MKMapview的缩放级别?

来自分类Dev

如何在PowerMock SuppressStaticInitializationFor注释中具有多个类

来自分类Dev

如何在Groovy中为多个目标定义注释?

来自分类Dev

如何在Groovy中为多个目标定义注释?

来自分类Dev

如何在PowerMock SuppressStaticInitializationFor注释中具有多个类

来自分类Dev

如何在wordpress的注释页面中显示用户[Commentator]角色

来自分类Dev

Swift:如何在iOS UIWebView中显示具有多个页面的远程PDF文件?

来自分类常见问题

如何在Android中显示多个通知

来自分类Dev

如何在ImageView中显示多个图像

来自分类Dev

如何在textView中显示多个值

来自分类Dev

如何在reactjs中显示多个组件

Related 相关文章

  1. 1

    如何在Mkmapview中拖放注释图钉

  2. 2

    如何在iOS MKMapView中设置标记

  3. 3

    无法在MKMapView中显示多个注释图钉

  4. 4

    如何在MKMapview上快速显示MKPinAnnotationView

  5. 5

    如何在OS X中打印MKMapView?

  6. 6

    如何在MKMapView上显示默认位置指示器

  7. 7

    如何在iOS MKMapView上达到最大缩放级别

  8. 8

    如何在MKMapView Swift中添加叠加路径

  9. 9

    如何在MKMapView中的MKAnnotation上设置图像

  10. 10

    如何在MKMapView中添加自定义透明MKAnnotation?

  11. 11

    如何在mkmapview中查找用户的当前位置

  12. 12

    如何在MKMapView中添加大半径标注?

  13. 13

    如何在Adobe Acrobat Pro XI中添加引用多个突出显示文本的注释?

  14. 14

    iOS如何在点击后使MKMapView在MKAnnotationView标注附件上居中?

  15. 15

    iOS如何在点击后使MKMapView在MKAnnotationView标注附件上居中?

  16. 16

    如何在Firebug中显示HTML注释?

  17. 17

    使用自定义图块时如何在MKMapView中删除或隐藏背景

  18. 18

    如何使用注释降低MKMapview的缩放级别?

  19. 19

    如何使用注释降低MKMapview的缩放级别?

  20. 20

    如何在PowerMock SuppressStaticInitializationFor注释中具有多个类

  21. 21

    如何在Groovy中为多个目标定义注释?

  22. 22

    如何在Groovy中为多个目标定义注释?

  23. 23

    如何在PowerMock SuppressStaticInitializationFor注释中具有多个类

  24. 24

    如何在wordpress的注释页面中显示用户[Commentator]角色

  25. 25

    Swift:如何在iOS UIWebView中显示具有多个页面的远程PDF文件?

  26. 26

    如何在Android中显示多个通知

  27. 27

    如何在ImageView中显示多个图像

  28. 28

    如何在textView中显示多个值

  29. 29

    如何在reactjs中显示多个组件

热门标签

归档