您的位置:首页 > 其它

MapKit学习笔记及源码分享

2012-01-09 14:58 309 查看
本文由论坛会员leonbao2010分享

1、概述

插入MapView,设置Delegate(一般为Controller),Annotations记录兴趣位置点(AnnotationView用来显示兴趣位置点),annotation是可选的,选中的annotation会显示callout,用来显示信息。

2、设置地图显示类型:

mapView.mapType = MKMapTypeStandard;

mapView.mapType = MKMapTypeSatellite;

mapView.mapType = MKMapTypeHybrid;

3、显示用户位置

设置为可以显示用户位置:

mapView.showsUserLocation = YES;

判断用户当前位置是否可见(只读属性):

userLocationVisible

得到用户位置坐标:当userLocationVisible为YES时

CLLocationCoordinate2D coords = mapView.userLocation.location.coordinate;

4、坐标范围

MKCoordinateRegion用来设置坐标显示范围。

包括两部分:Center(CLLocationCoordinate2D struct,包括latitude和longitude),坐标中心

和Span(MKCoordinateSpan struct,包括latitudeDelta和longitudeDelta),缩放级别

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(center,2000, 2000);

以上代码创建一个以center为中心,上下各1000米,左右各1000米得区域,但其是一个矩形,不符合MapView的横纵比例

MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];

以上代码创建出来一个符合MapView横纵比例的区域

[mapView setRegion:adjustedRegion animated:YES];

以上代码为:最终显示该区域

5、Delegate

使用MapView须符合MKMapViewDelegate协议

5.1、地图加载Delegate

当需要从Google服务器取得新地图时

mapViewWillStartLoadingMap:

当成功地取得地图后

mapViewDidFinishLoadingMap:

当取得地图失败后(建议至少要实现此方法)

mapViewDidFailLoadingMap:withError:
5.2、范围变化Delegate

当手势开始(拖拽,放大,缩小,双击)

mapView:regionWillChangeAnimated:

当手势结束(拖拽,放大,缩小,双击)

mapView:regionDidChangeAnimated:

判断坐标是否在MapView显示范围内:

CLLocationDegrees leftDegrees = mapView.region.center.longitude –(mapView.region.span.longitudeDelta / 2.0);

CLLocationDegrees rightDegrees = mapView.region.center.longitude +(mapView.region.span.longitudeDelta / 2.0);

CLLocationDegrees bottomDegrees = mapView.region.center.latitude –(mapView.region.span.latitudeDelta / 2.0);

CLLocationDegrees topDegrees = self.region.center.latitude +(mapView.region.span.latitudeDelta / 2.0);

if (leftDegrees > rightDegrees) { // Int'l Date Line in View

leftDegrees = -180.0 - leftDegrees;

if (coords.longitude > 0) // coords to West of Date Line

coords.longitude = -180.0 - coords.longitude;

}

If (leftDegrees
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: