gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 06:22:58 25 4
gpt4 key购买 nike

我是 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];
}

如果稍微缩小一点,您会看到所有三个图钉:

screenshot of demo app

关于ios - 如何在 MKMapView iOS 中显示多个注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27163318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com