地图框架

使用苹果自带的地图框架,需要在项目中加载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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* *author:XudongChen *Date:2010-03-09 *QQ:233828249 81023617(不才) *Email:xznd@163.com */ 2009-8-13 1.加载分块地图 2.添加全景标记窗体 2009-8-14 1.增加控制条 2.增加全景标记、公交车标记显示层 3.解决地图定位问题 4.增加经纬度层功能 5.未修正图标层的定位 2009-8-15 1.已修正8.14地图定位错误,还存在放大缩小时定位不准 2.存在ie内存泄漏问题 2009-8-16 1.已修正8.15 ie内存泄漏问题,chrome下可能还存在内存泄漏 2.增加图标定位功能 3.增加鼠标滚轮事件(http://yongzhi.blog.hexun.com/5057947_d.html) 4.通过jquery加载json数据文件(图标显示层数据) 2009-8-17 1.增加建筑物高亮显示(还需完成鼠标mouseover和mouseout事件) 2009-8-18 1.完成建筑物高亮显示,有点小bug 2009-8-19 1.增加记录原始缩放比例的全局变量 2.解决建筑物高亮显示bug 2009-8-20 1.增加三维全景展示功能 2.浏览建筑详细功能 2009-8-21 1.引入jqueryAlert插件,美化弹出窗体 2009-8-23 1.增加搜索功能 2.清理建筑信息显示页和公交信息页多余数据 2009-10-22 1.增加小沙盘拖动类(鹰眼视图) BirdEye.js 完成小沙盘到地图的同步,同步方法:检测mouseup事件触发->修改url->request->计算坐标->同步行为 2.增加小沙盘样式表BirdEye.css 地图图片路径birdeyemap 3.完成window.parent地图->小沙盘的同步 问题:小沙盘->window.parent地图存在bug,可以尝试开启 2009-10-23 1.在小沙盘中增加浮动绿色框 2009-11-6 1.实现“鹰眼地图”不需移动,一幅可以看到见全景, 当主场景移动时,“鹰眼地图”只有小框在移动。 同时“小框” 主场景也在移动。 2009-12-17 1.测距功能事件配置 2009-12-21 1.完成测距功能 2009-12-22 1.配置搜索功能,后台改用s2sh框架 2.完成hessian+spring+hibernate整合,提供建筑信息和公司信息hessian查找服务 2009-12-23 1.配置hessian服务端缓存 2.配置hessian日志记录,输出到文件/log/wzucxd/html 3.完成建筑信息显示页,配置2级缓存 4.配置oscache 5.配置新闻信息模块 2010-1-13 1.完成chrome和ff下的搜索功能 2.搜索功能支持ie6.0+ //设置Theodolite$setPoint var cpointtmp = new CPoint(this.holder.offsetLeft + evt.clientX - this.mvl.offsetLeft, this.mvl.offsetTop + evt.clientY - this.mvl.offsetTop); Theodolite$setPoint(cpointtmp);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值