MKMapView Class Reference
原文地址
An MKMapView object provides an embeddable map interface, similar to the one provided by the Maps application. You use this class as-is to display map information and to manipulate the map contents from your application. You can center the map on a given coordinate, specify the size of the area you want to display, and annotate the map with custom information.
MKMapView对象(object)提供了一个与地图应用类似的可嵌入的地图接口。在你的应用中,你可以用这个class来展示地图信息、操作地图内容。你可以使地图定位在一个给定的坐标(CLLocationCoordinate2D)上,指定你想要显示的地图的范围(MKCoordinateSpan,MKCoordinateRegion),还有在地图上标注(MKAnnotationView,MKPointAnnotation)你自定义的信息。
Important The Map Kit framework uses Google services to provide map data. Use of this class and the associated interfaces binds you to the Google Maps/Google Earth API terms of service. You can find these terms of service at http://code.google.com/apis/maps/iphone/terms.html.
重点:地图框架(库)(MapKit.framework)使用了谷歌服务器来获得数据。当你使用这个类class和相关的接口的时候要受到谷歌地图服务器的限制,你可以在这里找到这些条款。
When you initialize a map view, you should specify the initial region for that map to display. You do this by setting the region property of the map. A region is defined by a center point and a horizontal and vertical distance, referred to as the span. The span defines how much of the map at the given point should be visible and is also how you set the zoom level. Specifying a large span results in the user seeing a wide geographical area and corresponds to a low zoom level. Specifying a small span results in the user seeing a more narrow geographical area and corresponds to a higher zoom level.
在你初始化一个map view的时候,需要指定地图显示的范围。你可以通过设置地图的范围属性(MKCoordinateRegion)来完成这个操作。这个范围属性由一个中心点(一个经纬度CLLocationCoordinate2D)和一个跨度span(水平和垂直的距离MKCoordinateSpan)组成。这个跨度定义了地图上以给定坐标为中心的可见范围,和缩放级别。指定一个大的跨度使得可见范围大而缩放级别低,指定一个小的跨度使得可见范围小而缩放级别高。
代码解释:
CLLocationCoordinate2D coords;
coords.latitude = 39.000000 ;
coords.longitude = 116.0000000 ;
MKCoordinateSpan span = MKCoordinateSpanMake(0.1255, 0.2340);//地图的缩放,用来显示大头针周围的区域的大小,值越大显示的区域愈大
MKCoordinateRegion region = MKCoordinateRegionMake(coords, span);
[mapView setRegion:region animated:YES];
In addition to setting the span programmatically, the MKMapView class supports many standard interactions for changing the position and zoom level of the map. In particular, map views support flick and pinch gestures for scrolling around the map and zooming in and out. Support for these gestures is enabled by default but can also be disabled using the scrollEnabled and zoomEnabled properties.
除了设置跨度,MKMapView类class还支持一些标准的交互来改变地图的位置和缩放级别。特别的是,mapview支持手指快速滑动和捏合的手势来改变地图显示范围和缩放比例(住:这里与原文解释出入较大,为自己的理解)。默认情况下,这些都是支持的,但是同时你也可以使用scrollEnabled和zoomEnabled属性来禁用。
代码解释:
mapView.zoomEnabled = YES;//默认为YES,地图是否可缩放
mapView.scrollEnabled = YES;//默认为yes,地图是否可滚动显示
In iOS 4.0 and later, you can also use projected map coordinates instead of regions to specify some values. When you project the curved surface of the globe onto a flat surface, you get a two-dimensional version of a map where longitude lines appear to be parallel. Locations and distances on this map are specified using the MKMapPoint, MKMapSize, and MKMapRect data types. You can use these data types to specify the map’s visible region and when specifying the location of overlays.
在ios4.0以后,你可以用投影坐标来代替范围来指定一些数值。当你将地图的曲面投射到一个平面上的时候,你会得到一个地图的二维平面(version意译),在这个平面上地球的经度几乎平行。地图上的位置和距离是MKMapPoint,MKMapSiz和MKMapRect类型的,你可以用这些类型的数据来指定地图的可是区域和位置重叠的视图。(这个投影没用过,翻译可能有较大偏差)。
以下为相关类型注解:
// An MKMapPoint is a coordinate that has been projected for use on the
// two-dimensional map. An MKMapPoint always refers to a place in the world
// and can be converted to a CLLocationCoordinate2D and back.
MKMapPoint 是一个用在二维地图上的坐标,通常是代表了世界上的某一个地方,它可以和CLLocationCoordinate2D类型互相转化
typedefstruct {
double x;
double y;
} MKMapPoint;
typedefstruct {
double width;
double height;
} MKMapSize;
typedefstruct {
MKMapPoint origin;
MKMapSize size;
} MKMapRect;
Although you should not subclass the MKMapView class itself, you can get information about the map view’s behavior by providing a delegate object. The map view calls the methods of your custom delegate to let it know about changes in the map status and to coordinate the display of custom annotations, which are described in more detail in “Annotating the Map.” The delegate object can be any object in your application as long as it conforms to the MKMapViewDelegate protocol. For more information about implementing the delegate object, see MKMapViewDelegate Protocol Reference.
虽然你不能直接继承于MKMapView,但是你可以通过地图的代理方法来捕捉地图的信息(个人理解为行为信息,比如加标注,缩放,加载等)。当地图的状态发生改变和展示自定义的注解的时候,mapview会调用代理中的方法,让这些方法知道地图的这些变化,这些方法定义在“Annotation the Map”中。只要符合MKMapViewDelegate属性,委托对象可以是你应用中的任意一个对象。更多的相关信息,查看MKMapViewDelegateProtocolReference.(意译较多,可能出入较大)
Annotating the Map
注释地图
The MKMapView class supports the ability to annotate the map with custom information. Because a map may have potentially large numbers of annotations, map views differentiate between the annotation objects used to manage the annotation data and the view objects for presenting that data on the map.
MKMapView 类可以用自定义的信息来注释地图。因为一个地图上可能会含有大量的注释信息,所以地图会利用两个对象(object)来区分这些注释,一个对象是用来管理注释信息的annotation object,另一个是view object ,这个对象展示了地图上的信息。
An annotation object is any object that conforms to the MKAnnotationprotocol. Annotation objects are typically implemented using existing classes in your application’s data model. This allows you to manipulate the annotation data directly but still make it available to the map view. Each annotation object contains information about the annotation’s location on the map along with descriptive information that can be displayed in a callout.
只要符合MKAnnotation这个协议,那么annotation object可以是任何一个对象。比如MKPointAnnotation(个人理解,不能确定)。Annotation object通常使用你的应用中已经存在的数据类型。这允许你直接操作注释信息,也可以在地图中使用。每个annotation object都包含了标注在地图上的位置信息和显示在标注上的描述信息。
The presentation of annotation objects on the screen is handled by an annotation view, which is an instance of theMKAnnotationView class. An annotation view is responsible for presenting the annotation data in a way that makes sense. For example, the Maps application uses a pin icon to denote specific points of interest on a map. (The Map Kit framework offers theMKPinAnnotationView class for similar annotations in your own applications.) You could also create annotation views that cover larger portions of the map.
在屏幕内所有annotation object的描述都是由annotation view来控制的,这个view是一个MKAnnotationView类。annotation view负责显示标注的信息。例如,地图的应用用一个大头针的图标来表示地图上的一个特定的点(地图框架提供了MKPinAnnotationView类用于应用中的注释)。你也可以创建一个在地图上覆盖了较大部分的区域的注释视图view。
Because annotation views are needed only when they are onscreen, the MKMapView class provides a mechanism for queueing annotation views that are not in use. Annotation views with a reuse identifier can be detached and queued internally by the map view when they move offscreen. This feature improves memory use by keeping only a small number of annotation views in memory at once and by recycling the views you do have. It also improves scrolling performance by alleviating the need to create new views while the map is scrolling.
因为只有当注释视图annotation view在屏幕上的时候我们才需要,所以MKMapView类提供了一种机制,这种机制是的当前不适用的annotation view排队等待。带有复用标志的annotation view在移除屏幕的时候会在内部队列中等待。这种特性利用两种方法来提高了内存的使用,方法一为在某一时刻只使用了很少数的annotation view,方法二为重复利用已经有的view。在地图滚动的时候,由于减少了不必要的重新创建新的view的过程,而提高了滚动的效率。
When configuring your map interface, you should add all of your annotation objects right away. The map view uses the coordinate data in each annotation object to determine when the corresponding annotation view needs to appear onscreen. When an annotation moves onscreen, the map view asks its delegate to create a corresponding annotation view. If your application has different types of annotations, it can define different annotation view classes to represent each type.
配置你的地图页面时,你最好立刻添加你所有的注释对象annotation objects。地图需要使用在注释对象annotation object里面的坐标信息来确定相应的注释视图annotation view要在什么时候出现在屏幕上。当一个注释进入到屏幕的时候,地图会执行它的代理方法来穿件一个相应的注释视图annotation view。如果你的应用有不同类型的注释,他会定义不同的注释视图annotation view 来展示每一种类型。
解释说明:
代码例子:
MKPointAnnotation *pointAnnotation = [[MKPointAnnotationalloc]init];
pointAnnotation.coordinate = coord;
pointAnnotation.title = @“title”;
pointAnnotation.subtitle = @“subtitle”;
[mapView addAnnotation:pointAnnotation];
MKPointAnnotation继承于MKShape,MKShape继承于NSObject同时实现MKAnnotation接口。
如果 要给地图添加大头针,也就是注释,需要在初始化地图的时候就设定好注释对象。这个类有三个属性,大头针的坐标,标题和副标题。在添加这个注释对象之后,地图会发送通知给代理方法
// mapView:viewForAnnotation: provides the view for each annotation.
// This method may be called for all or some of the added annotations.
// For MapKit provided annotations (eg. MKUserLocation) return nil to use the MapKit provided annotation view.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
//这个方法为每一个注释的对象提供一个展示的平台MKAnnotationView
//当地图添加注释的时候这个方法会被地图调用
//
代码例子:
- (MKAnnotationView *)mapView:(MKMapView *)amapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSString *pinString = @"pin";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[amapView dequeueReusableAnnotationViewWithIdentifier:pinString];
if (pinView == nil) {
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:pinString];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
[pinView setSelected:YES animated:YES];
pinView.selected = YES;
}
pinView.pinColor = 2;
return pinView;
}
MKPinAnnotationView继承于MKAnnotationView。最好使用复用,减少内存消耗。
Adding Overlays to the Map
添加覆盖
In iOS 4.0 and later, you can use overlays to display content over a wide portion of the map. An overlay is any object that conforms to the MKOverlay protocol. An overlay object is a data object that contains the points needed to specify the shape and size of the overlay and its location on the map. Overlays can represent shapes such as circles, rectangles, multi-segment lines, and simple or complex polygons. You can also define your own custom overlays to represent other shapes.
在4.0以后,你可以用覆盖层来显示一些包含地图很大部分的内容。只要符合MKOverlay属性,那么覆盖层可以是任何对象。一个覆盖对象是一个数据对象,它包含了覆盖层的形状,大小和它在地图上的位置。覆盖层可以是圆形,方形,多线段,简单或复杂的多边形。你也可以自定义一个其他形状的覆盖层。
The presentation of an overlay onscreen is handled by an overlay view, which is an instance of the MKOverlayView class. The job of an overlay view is to draw the shape representing the overlay on top of the map content. For example, an overlay that represents a bus route might have an overlay view that draws the path of the route along with icons showing the stops along that route. The Map Kit framework defines overlay views for the standard types of overlay objects and you can define additional overlay views as needed.
屏幕上的覆盖层是由一个覆盖视图overlay view控制的,这个视图是继承于MKOverlayView类的。一个覆盖视图的工作就是在地图的上方描绘覆盖曾的形状。例如,一个用来显示公交路线的覆盖层,应该有一个覆盖视图来根据沿途的站点来画路径。地图框架为覆盖对象标准类型定义了覆盖视图,你也可以自定义额外的覆盖视图。
When configuring your map interface, you can add overlay objects at any time. The map view uses the data in each overlay object to determine when the corresponding overlay view needs to appear onscreen. When an overlay moves onscreen, the map view asks its delegate to create a corresponding overlay view.
当你配置你的地图页面时,你可以在任何时候添加覆盖对象。赌徒需要用覆盖对象中的数据来决定相应的福彩视图出现在屏幕中。当一个覆盖曾进入屏幕的时候,地图会调用代理方法来创建相应的覆盖视图。
Tasks 任务
Accessing Map Properties 地图属性
- mapType property
- zoomEnabled property
- scrollEnabled property
Accessing the Delegate 代理
- delegate property
Manipulating the Visible Portion of the Map 操作地图可见区域
- region property
- – setRegion:animated:
- centerCoordinate property
- – setCenterCoordinate:animated:
- visibleMapRect property
- – setVisibleMapRect:animated:
- – setVisibleMapRect:edgePadding:animated:
Accessing the Device’s Current Location 设备当前位置
- showsUserLocation property
- userLocationVisible property
- userLocation property
Annotating the Map 注释地图
- annotations property
- – addAnnotation:
- – addAnnotations:
- – removeAnnotation:
- – removeAnnotations:
- – viewForAnnotation:
- – annotationsInMapRect:
- annotationVisibleRect property
- – dequeueReusableAnnotationViewWithIdentifier:
Managing Annotation Selections 注释选择管理
Adding and Removing Overlays 添加和移除覆盖
- overlays property
- – addOverlay:
- – addOverlays:
- – removeOverlay:
- – removeOverlays:
- – insertOverlay:atIndex:
- – exchangeOverlayAtIndex:withOverlayAtIndex:
- – insertOverlay:aboveOverlay:
- – insertOverlay:belowOverlay:
- – viewForOverlay:
Converting Map Coordinates 改变地图坐标
- – convertCoordinate:toPointToView:
- – convertPoint:toCoordinateFromView:
- – convertRegion:toRectToView:
- – convertRect:toRegionFromView:
Adjusting Map Regions and Rectangles
Tracking the User Location 跟踪用户位置
Properties 属性
annotations 注释
The complete list of annotations associated with the receiver. (read-only)
注释的完整的列表
@property(nonatomic, readonly) NSArray *annotations
Discussion
The objects in this array must adopt the MKAnnotation protocol. If no annotations are associated with the map view, the value of this property is nil.
在这个数组里面的对象必须都是MKAnnotation属性的。如果地图没有注释的话,这个属性为空nil。
Availability
- Available in iOS 3.0 and later. 3.0以上版本可用
See Also
Related Sample Code 相关例子
Declared In 定义在MKMapView.h中
MKMapView.h
annotationVisibleRect 注释可见范围
The visible rectangle where annotation views are currently being displayed. (read-only) 注释视图当前展示的可见范围
@property(nonatomic, readonly) CGRect annotationVisibleRect
Availability
- Available in iOS 3.0 and later.
Declared In
MKMapView.h
centerCoordinate 中心坐标
The map coordinate at the center of the map view.
@property(nonatomic) CLLocationCoordinate2D centerCoordinate
Discussion
Changing the value in this property centers the map on the new coordinate without changing the current zoom level. It also updates the values in the regionproperty to reflect the new center coordinate and the new span values needed to maintain the current zoom level.
改变这个属性可以在不改变地区当前缩放的情况下用新的坐标重新设置地图的中心点。同时改变这个属性也会更改范围region这个属性,包括region属性的新的中心坐标和新的跨度。
Changing the value of this property updates the map view immediately. If you want to animate the change, use the setCenterCoordinate:animated:method instead.
改变这个属性会立刻更新地图。如果你想在改变地图属性的时候添加动画效果,可以使用setCenterCoordinate:animated:这个方法
Availability
- Available in iOS 3.0 and later.
See Also
Declared In
MKMapView.h
delegate 委托(代理)
The receiver’s delegate.
@property(nonatomic, assign) id<MKMapViewDelegate> delegate
Discussion
A map view sends messages to its delegate regarding the loading of map data and changes in the portion of the map being displayed. The delegate also manages the annotation views used to highlight points of interest on the map.
当地图加载信息和更改可视范围的时候,地图会向它的委托方法发送消息。同时,代理方法也管理自定义的注释视图。
The delegate should implement the methods of the MKMapViewDelegate protocol.
代理方法必须在MKMapViewDelegate协议里面执行
Availability
- Available in iOS 3.0 and later.
Declared In
MKMapView.h
mapType 地图类型(地球人都知道,不多说。)
The type of data displayed by the map view.
@property(nonatomic) MKMapType mapType
Discussion
Changing the value in this property may cause the receiver to begin loading new map content. For example, changing from MKMapTypeStandard toMKMapTypeSatellite might cause it to begin loading the satellite imagery needed for the map. If new data is needed, however, it is loaded asynchronously and appropriate messages are sent to the receiver’s delegate indicating the status of the operation.
异步加载信息
Availability
- Available in iOS 3.0 and later.
Related Sample Code
Declared In
MKMapView.h
overlays 覆盖
The overlay objects currently associated with the map view. (read-only)
@property(nonatomic, readonly) NSArray *overlays
Discussion
The objects in this array must adopt the MKOverlay protocol. If no overlays are associated with the map view, the value of this property is an empty array.
在这个数组里面的对象必须符合MKOverlay类。如果没有,这个属性为空nil。
If the regions defined by two overlays intersect one another, the order of the objects in this array determines the z-order of the corresponding overlay views that are displayed in the map. Overlay objects at the beginning of the array are placed behind those that come later in the array. Thus, the view for an overlay at index 0 is displayed behind the view for the overlay at index 1.
如果某个区域由两个覆盖层组成,那么这个数组里面的对象的顺序绝顶了地图上相应覆盖层的z顺序。在这个数组里面最开始的覆盖对象在后面的对象的下面。因此,在0位置的覆盖视图会在处于1位置的视图的下方。
Availability
- Available in iOS 4.0 and later.
Declared In
MKMapView.h
region 区域 (范围)
The area currently displayed by the map view.地图上当前显示的区域范围
@property(nonatomic) MKCoordinateRegion region
Discussion
The region encompasses both the latitude and longitude point on which the map is centered and the span of coordinates to display. The span values provide an implicit zoom value for the map. The larger the displayed area, the lower the amount of zoom. Similarly, the smaller the displayed area, the greater the amount of zoom.
region属性包含了地图中心坐标和地图跨度的经纬度。跨度span属性为地图提供了一个不明显的缩放值zoom value。zoom缩放级别越低,显示范围越大。同样的,缩放级别越高,显示的范围越小。
Changing only the center coordinate of the region can still cause the span to change implicitly. The span might change because the distances represented by a span change at different latitudes and longitudes and the map view may need to adjust the span to account for the new location. If you want to change the center coordinate without changing the zoom level, use the centerCoordinate instead.
只改变区域的中心坐标同样会使得跨度不明显的改变。这是因为一个不同的经纬度的跨度的距离已经改变,还有地图为了重新加载新的地址信息也需要重新判断跨度。如果只想改变地图的中心点而不改变地图的缩放级别,请是使用centerCoordinate这个属性替代
Changing the value of this property updates the map view immediately. When setting this property, the map may adjust the new region value so that it fits the visible area of the map precisely. This is normal and is done to ensure that the value in this property always reflects the visible portion of the map. However, it does mean that if you get the value of this property right after setting it, the returned value may not match the value you set. (You can use theregionThatFits: method to determine the region that will actually be set by the map.)
改变地图的region这个属性会使地图立刻更新。当改变这个属性的时候,地图会判断新的范围值来精准的适应地图的可见范围。这是为了确定这个属性的值可以正确的反应地图的可见区域。但是,同时这也意味着如果你在设置这个属性后就去获取这个值的话,返回的值可能并不是你刚刚所赋的值。(你可以用regionThatFits:这个方法来查看)
If you want to animate the change in region, use the setRegion:animated: method instead.
如果你想在改变范围的时候添加动画,请用setRegion:animated:这个方法
Availability
- Available in iOS 3.0 and later.
See Also
Related Sample Code
Declared In
MKMapView.h
scrollEnabled 可滚动么?布尔属性(地球人都知道,不多说)
A Boolean value that determines whether the user may scroll around the map.
@property(nonatomic, getter=isScrollEnabled) BOOL scrollEnabled
Discussion
This property controls only user interactions with the map. If you set the value of this property to NO, you may still change the map location programmatically by changing the value in the region property.
The default value of this property is YES.
Availability
- Available in iOS 3.0 and later.
Declared In
MKMapView.h
selectedAnnotations
The annotations that are currently selected. 当前选择的标注
@property(nonatomic, copy) NSArray *selectedAnnotations
Discussion
Assigning a new array to this property selects only the first annotation in the array.
Availability
- Available in iOS 3.0 and later.
Declared In
MKMapView.h
showsUserLocation 是否显示用户当前位置,布尔类型
A Boolean value indicating whether the map may display the user location.
@property(nonatomic) BOOL showsUserLocation
Discussion
This property does not indicate whether the user’s position is actually visible on the map, only whether the map view is allowed to display it. To determine whether the user’s position is visible, use the userLocationVisible property. The default value of this property is NO.
这个属性并不是立刻就在地图上显示用户的位置,只有当地图map view允许显示的时候才显示。如果想要判断用户当前的位置是否是可见的,可以使用userLocationVisible这个属性,默认情况是no。
Setting this property to YES causes the map view to use the Core Location framework to find the current location. As long as this property is YES, the map view continues to track the user’s location and update it periodically.
设置这个属性为yes会使用 Core Location framework 这个框架来查找用户当前的位置(所以,你懂的!加库呀!)。当这个属性为yes的时候,地图会跟踪用户的位置,而且会定时的更新用户位置。
PS:这个方法- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation NS_AVAILABLE(NA, 4_0)
可以看到用户的位置信息
Availability
- Available in iOS 3.0 and later.
See Also
Declared In
MKMapView.h
userLocation 用户位置(说的太多了,我都不想说了)
The annotation object representing the user’s current location. (read-only)
@property(nonatomic, readonly) MKUserLocation *userLocation
Availability
- Available in iOS 3.0 and later.
See Also
Declared In
MKMapView.h
userLocationVisible 用户位置是否可见(上文提到过,pass)
A Boolean value indicating whether the device’s current location is visible in the map view. (read-only)
@property(nonatomic, readonly, getter=isUserLocationVisible) BOOL userLocationVisible
Discussion
This property uses the horizontal accuracy of the current location to determine whether the user’s location is visible. Thus, this property is YES if the specific coordinate is offscreen but the rectangle surrounding that coordinate (and defined by the horizontal accuracy value) is partially onscreen.
If the user’s location cannot be determined, this property contains the value NO.
Availability
- Available in iOS 3.0 and later.
Declared In
MKMapView.h
userTrackingMode 跟踪呼呼位置(没用过,不知道,以后更新)
The mode used to track the user location.
@property(nonatomic) MKUserTrackingMode userTrackingMode
Discussion
Possible values are described in “MKUserTrackingMode.”
Availability
- Available in iOS 5.0 and later.
See Also
Declared In
MKMapView.h
visibleMapRect 地图当前可见区域(类似region属性,不多说)
The area currently displayed by the map view.
@property(nonatomic) MKMapRect visibleMapRect
Discussion
This property represents the same basic information as the region property but specified as a map rectangle instead of a region.
Changing the value of this property updates the map view immediately. If you want to animate the change, use the setVisibleMapRect:animated: method instead.
Availability
- Available in iOS 4.0 and later.
Declared In
MKMapView.h
zoomEnabled 是否可以缩放(pass)
A Boolean value that determines whether the user may use pinch gestures to zoom in and out of the map.
@property(nonatomic, getter=isZoomEnabled) BOOL zoomEnabled
Discussion
This property controls only user interactions with the map. If you set the value of this property to NO, you may still change the zoom level programmatically by changing the value in the region property.
The default value of this property is YES.
Availability
- Available in iOS 3.0 and later.
Declared In
MKMapView.h