地图框架

简介: <div style="font-family:Arial; font-size:14px; line-height:26px">   http://blog.csdn.net/debolee/article/details/44886209<br></div> <div style="font-family:Arial; font-size:14px; line-height:26p
   http://blog.csdn.net/debolee/article/details/44886209

使用苹果自带的地图框架,需要在项目中加载MapKit.framework(项目—TARGETS--Build Phases--Link Binary With Libraries),并在头文件中导入 #import <MapKit/MapKit.h>

一、MKMapView的常用属性和方法
NSArray *annotations;//保存地图中的大头针
MKCoordinateRegion region; //显示区域
MKCoordinateRegion是个结构体,包括两个属性
typedefstruct {
CLLocationCoordinate2D center;
MKCoordinateSpan span;
} MKCoordinateRegion;

CLLocationCoordinate2D//显示经纬度
MKCoordinateSpan//显示精度

// 设置显示位置,显示在屏幕中心点
    CLLocationCoordinate2D coord;
    coord.longitude = 113.346196//经度
    coord.latitude = 23.140563;//纬度
//地图的显示精度,数值越小地图显示越详细
    MKCoordinateSpan span;
    span.longitudeDelta = 0.1;
    span.latitudeDelta = 0.1;
//设置显示区域
    [self.mapViewsetRegion:(MKCoordinateRegionMake(coord, span))];

// 点击屏幕上的位置,获取经纬度
- (
IBAction )tapAction:( UITapGestureRecognizer *)sender {
   
  CGPoint  p = [sender locationInView : self . view ];
   
  // 将屏幕上的点转换为地图坐标
   
  CLLocationCoordinate2D  coord = [ self . mapView convertPoint :p toCoordinateFromView : self . view ];
   
  NSLog ( @"coord.longitude:%lf coord.latitude:%lf" , coord. longitude ,coord. latitude );
}

二、在地图上设置大头针
在地图上显示大头针,苹果是通过遵守一个 MapKit 框架中 MKAnnotation 协议来实现的。
1、创建一个继承自NSObject的子类
2、在子类中导入 #import <MapKit/MapKit.h>
3、让子类遵守  MKAnnotation 协议,实现协议中的属性和方法  
// 必须实现
@property ( nonatomic , readonly ) CLLocationCoordinate2D coordinate;

// 选择实现
// Title and subtitle for use by selection UI.
@property ( nonatomic , copy ) NSString *title;
@property ( nonatomic , copy ) NSString *subtitle;

// Called as a result of dragging an annotation view.
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;

4、在地图中添加大头针
//创建一个大头针
XYZAnnotation*ann = [[XYZAnnotationalloc]init];
//设置经纬度等属性
[annsetCoordinate:coord];
ann.title= @"华师地铁站";
ann.subtitle= @"这里是华师地铁站";
//往地图上添加一个大头针
[self.mapViewaddAnnotation:ann];
// 获取地图中的大头针
NSArray *anns = [ self . mapView annotations ];
//移除地图中所有的大头针
[self.mapViewremoveAnnotations:anns];

5、自定义大头针视图
  • 创建一个继承自MKAnnotationView的自定义大头针视图
@interface XYZAnnotationView :MKAnnotationView

@end

#import"XYZAnnotationView.h"
@implementation XYZAnnotationView

- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithAnnotation:annotationreuseIdentifier:reuseIdentifier];
    if (self) {
       UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(-15, -30,30,30)]; //点击屏幕放置大头针,设置大头针的frame.origin 修正偏移
        imageView.image = [UIImageimageNamed:@"pink.png"];
        [self addSubview:imageView];
    }
    return self;
}
@end

  • 当前视图控制器遵守 MKMapViewDelegate协议;
  • 设置mapView的委托对象为当前视图控制器(self.mapView.delegate= self或者在Storyboard中拖拽);
  • 实现协议方法 
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    static NSString *annotationViewId = @"annotationView";
   
    //从队列中获取一个annotationView
    XYZAnnotationView *annotationView = (XYZAnnotationView *)[self.mapViewdequeueReusableAnnotationViewWithIdentifier:annotationViewId];
   
    //如果队列中没有,则创建一个新的annotationView
    if (!annotationView) {
        annotationView = [[XYZAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:annotationViewId];
    }
   
    return annotationView;
}

目录
相关文章
|
Nacos 数据安全/隐私保护
nacos启用鉴权后curl调用接口
nacos启用鉴权后curl调用接口
|
存储 数据可视化 Java
自定义注解实现aop切面
自定义注解实现aop切面
199 1
|
移动开发 API Android开发
构建高效Android应用:探究Kotlin协程的优势与实践
【4月更文挑战第7天】 在移动开发领域,性能优化和应用响应性的提升一直是开发者追求的目标。近年来,Kotlin语言因其简洁性和功能性在Android社区中受到青睐,特别是其对协程(Coroutines)的支持,为编写异步代码和处理并发任务提供了一种更加优雅的解决方案。本文将探讨Kotlin协程在Android开发中的应用,揭示其在提高应用性能和简化代码结构方面的潜在优势,并展示如何在实际项目中实现和优化协程。
|
JavaScript 前端开发 测试技术
那些你应该说再见的 npm 祖传老库
那些你应该说再见的 npm 祖传老库
221 0
|
弹性计算 运维 容灾
阿里云使用体验
经过进一步的体验云服务器我感受到阿里云提供的性能卓越、稳定可靠、弹性扩展的IaaS(Infrastructure as a Service)级别云计算服务。云服务器ECS免去了采购IT硬件的前期准备,就像是使用水、电、天然气等公共资源一样便捷、高效地使用服务器,实现计算资源的即开即用和弹性伸缩,在使用期间给阿里云我最大的印象就是方便,快捷。使用界面简单明了,通过不断地尝试,我了解到了更多的特殊概念,专业名词,也了解到了如快照等容灾备份能力的实际用途,现实生活中,如果我们的电脑磁盘出现了故障,数据出现了损坏就无能为力了,或者只能够找专业的人把数据能够找回来,但是不能够保证说所有的数据都能找回来。
|
4天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
14天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1306 5
|
13天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1336 87