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

2024-07-04

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

但 MKMapView 控制器中没有显示任何注释,请给我解决方案。


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

请注意,此方法纯粹是用代码完成的,没有界面生成器。

视图控制器.h

#import <MapKit/MapKit.h>

@interface ViewController : UIViewController <MKMapViewDelegate>

@property (nonatomic, strong) MKMapView *mapView;

@end

视图控制器.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];
}

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 MKMapView iOS 中显示多个注释? 的相关文章

  • 无法创建带有 nil 模型错误的 NSPersistentStoreCoordinator

    我有一个运行完美的 coreData 数据模型文件 由于一些特殊要求 我deleted旧的数据模型文件并创建另一个数据模型文件完全相同的实体 有实体没有变化来自之前的数据模型 我已将其作为不同的捆绑并从该包中引用它 用于创建 Managed
  • 使用RestKit执行同步请求

    BOOL do a Restkit request and return a boolean manager postObject nil path mypath parameters password password success R
  • UIApplication.delegate 必须仅在主线程中使用[重复]

    这个问题在这里已经有答案了 我的应用程序委托中有以下代码 作为在其他 viewController 中使用 CoreData 的快捷方式 let ad UIApplication shared delegate as AppDelegate
  • 应用内购买在 iOS 5 中不工作

    我已成功将应用程序购买集成到我的应用程序中 我已关注this http www raywenderlich com 21081 introduction to in app purchases in ios 6 tutorial应用程序内购
  • 如何在 Xcode 4 中使用 git 移动文件和文件夹?

    我熟悉 Xcode 中组和实际目录之间的差异 我总是在查找器中创建一个实际文件夹并将其拖到项目中 确保未选中 复制 当我移动文件夹时 我仅通过引用删除项目 将它们移动到查找器中 然后重新添加它们 现在我第一次使用git 在测试中发现 如果我
  • Xcode 7 UIWebView 不加载 url

    我在我的应用程序中使用 UIWebView 它在 Xcode4 5 6 模拟器中运行良好 但不适用于 Xcode 7 模拟器 我不知道为什么 模拟器中没有警告或错误 屏幕只显示空白页 请帮我 谢谢 import IndexViewContr
  • 在 Parse iOS 中链接用户 ID 和安装 ID

    在 Parse 仪表板的数据浏览器窗格中 我看到安装和用户类 但是 对于特定设备上的特定用户 objectId 不匹配 现在 用户订阅的频道仅在安装类中可见 有没有什么方法可以将用户 ID 来自用户类别 链接到安装 ID 以便可以知道用户订
  • 准备 iPhone 的调试器支持

    我无法在 iPhone 上安装该应用程序 我被这条消息困住了 准备 iPhone 的调试器支持 在这里尝试了所有答案 https stackoverflow com questions 46316373 xcode9 iphone is b
  • UIWebView 中自动填充用户名和密码

    我正在为我做一个非常简单的应用程序 当我启动这个应用程序时 它只会将我带到这个网页https social tre it expert https social tre it expert 我想自动登录 那么有没有办法自动填写用户名和密码
  • 嵌入了 iOS 项目的 OSX 项目 - 找不到 UIKit.h [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我有一个 OSX 项目和一个 iOS 子项目 子项目使用 UIKit h 但即使子项目已将 UIKit 框架添加到 链接二进制文件与库 部
  • 无法使用设备上的沙箱测试用户登录

    尝试进行一些应用程序内购买测试 因此我在 itunesconnect 上创建了一个沙箱测试器 但是当我尝试在我的设备上以该用户身份登录时 出现错误 不允许创建 Itunes 帐户 Apple ID 目前无法用于 iTunes 商店 请 稍后
  • 如何测试为 SKProductsRequest 设置不同的区域设置

    我正在 iPhone 应用程序中实现 IAP 并在沙盒环境中工作 我有我的代码设置要处理SKProducts由返回SKProductsRequest获取定价信息 我想测试检索不同区域设置中的价格值 例如欧元或日元 以测试我的格式化代码 我怎
  • Firebase 分析在 DebugView 中显示不正确的数据

    我正在使用 Firebase 分析 我注册了两个用户属性 并且有两个自定义事件 每个事件包含 3 到 50 个不同的选项 我遇到的问题是 当我设置用户属性时 DebugView 通常会显示事件的旧用户属性或根本不显示 有时它可以正常工作 请
  • Spotify 会话管理

    我在我的应用程序中有一个 Spotify 登录并尝试进行自动登录 登录功能 func getSpotifyToken fromController controller UIViewController success spotifyTok
  • 当使用密码锁定屏幕时,iOS 应用程序无法在后台访问文件

    Good day 我有一个 IOS 消息应用程序 该应用程序将接收和发送的消息存储在 sqlite3 数据库中 并且该应用程序还会生成日志文件 这可能发生在前台或后台任务中 当在 IOS 设备上禁用密码时 一切工作正常 即使屏幕关闭且应用程
  • iOS 9 中的触觉

    您可以在 iPhone 6s 上使用 iOS 9 中的 Taptic 引擎吗 WatchOS2 和 OS X 能够使用触觉引擎 所以我认为 iOS 9 中也会有 但我找不到任何 API 是的 我对内部进行了逆向工程UIKit我发现了另一种
  • Google 登录显示不正确的项目名称 - iOS

    我一直在集成 Google 登录 以便使用 Google Calendar API 同步日历事件 当我尝试登录时 我收到了这个消息 正如您所看到的 项目 应用程序的名称显示为 project xxxxxxxxx 而不是项目名称本身 如何在这
  • 删除/重置核心数据中的所有条目?

    您知道有什么方法可以删除存储在核心数据中的所有条目吗 我的架构应该保持不变 我只是想将其重置为空白 Edit 我希望以编程方式执行此操作 以便用户基本上可以点击reset button 您仍然可以使用 NSFileManager remov
  • RestKit:如何获取简单的 JSON 字符串数组?

    我应该如何使用 RestKit 来获取像这样的 JSON 字符串数组 Paris London Brussels New York 我尝试进行对象映射 但由于没有关键路径或属性 我不知道要映射什么 我什至不需要映射 结果可能只是一个数组或字
  • 删除 iOS 中的后台位置警报?

    我正在编写一个跟踪用户位置的导航应用程序 为了继续显示有关路线的通知 当手机锁定 接听电话等时 我还需要在应用程序处于后台时继续跟踪位置 Capabilities gt Background Modes gt Location update

随机推荐