现在的位置: 首页 > 综合 > 正文

IPhone mapView

2018年05月25日 ⁄ 综合 ⁄ 共 1730字 ⁄ 字号 评论关闭

一.基本知识

目前主流的智能手机大部分都支持GoogleMap地图程序,而手机上的地图程序确实能给我们的出行带来很大的方便。在iPhone中利用MapKit框架可以很方便的显示Google地图,并且可以在地图上添加标注。

二.具体介绍

1.MKMapView的显示

(1)创建MKMapView

CGRect rect = CGRectMake(0, 20, 320, 460);

MKMapView *mapView = [[MKMapView alloc] initWithFrame:rect];

(2)设定经纬度

CLLocationCoordinate2D theCoordinate;

theCoordinate.latitude=24.148926;

theCoordinate.longitude=120.715542;

(3)设定显示范围

MKCoordinateSpan theSpan;

theSpan.latitudeDelta=0.1;

theSpan.longitudeDelta=0.1;

(4)设置地图显示的中心及范围

MKCoordinateRegion theRegion;

theRegion.center=theCoordinate;

theRegion.span=theSpan;

(5)设置地图显示的类型及根据范围进行显示

[mapView setMapType:MKMapTypeStandard];

[mapView setRegion:theRegion];

完成这些步骤,再把mapView添加到当前view中就可以显示了。

2.在MKMapView上添加标注

(1)和标注相关的类及协议

(a)MKAnnotation Protocol

标注必须实现这个协议,有三个属性,coordinate,title和subtitle,其中coordinate属性必须设置。

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate

(b)MKAnnotationView

设置好Annotation后就可以用这个把标注在地图上显示出来,

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier

其比较重要的属性有

@property (nonatomic, retain) UIImage *image

自定义在地图上标注的图片

@property (nonatomic) BOOL canShowCallout

设置点击后能否弹出标注

@property (retain, nonatomic) UIView *rightCalloutAccessoryView

property (retain, nonatomic) UIView *leftCalloutAccessoryView

设置在标注的左右边点击后进一步弹出附属的View

(c)MKPinAnnotationView

这是以大头针的方式显示标注,继承自MKAnnotationView,同时添加了两个属性

@property (nonatomic) MKPinAnnotationColor pinColor

设置大头针的颜色,有红绿紫三种颜色可选择

@property (nonatomic) BOOL animatesDrop

设置大头针是否以掉下来的动画方式显示

(2)在地图上添加Annotation的步骤

(a)创建一个实现MKAnnotation协议的类,在该类的初始化函数中给其coordinate属性设置

(b)用上述方法创建Annotation

(c)把创建的Annotation用addAnnotation的方法添加到MapView中

(d)实现MKMapViewDelegate代理,在代理函数

- (MKAnnotationView *)mapView:(MKMapView *)mView viewForAnnotation:(id <MKAnnotation>)annotation中把Annotation以MKPinAnnotationView或 MKAnnotationView的方式标注在地图上上显示。

【上篇】
【下篇】

抱歉!评论已关闭.