一. MKMapView
1. MKAnnotationView : 包括坐标,标题,子标题,左侧图片,右侧按钮
2. 使用大头针,需要实现MKAnnotation 协议,
3. 添加大头针方法:
-(void)addAnnotation:(id<MKAnnotation>)annotation;
-(void)addAnnotations:(NSArray*)annotations;
-(void)removeAnnotation:(id<MKAnnotation>)annotation;
-(void)removeAnnotations:(NSArray*)annotations;
4. 如果知道所有annotation,推荐预先把所有Annotation加到MapView上。annotation Views 会被重用(类似UITableView cell),这样对性能有帮助。
5. MKAnnotationView 的创建
类似UITableView cell,code:
- (MKAnntationView*)mapView:(MKMapView*)sender
viewForAnnotation:(id<MKAnnotation>)annotation {
MKAnnotationView *aView = [sender dequeueReusableAnnotationViewWithIdentifier:IDENT];
if(!aView) {
aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:IDENT];
}
aView.annotation = annotation;
return aView;
}
6. 点击大头针时
- (void)mapView:(MKMapView*)sender didSelectAnnotationView:(MKAnnotationView*)aView {
if([aView.leftCalloutAccessoryView isKindOfClass:[UIImageView class]]) {
UIImageView *imageView = (UIImageView*)aView.leftCalloutAccessoryView;
imageView.image = ...; /// 可通过gcd获取图片
}
}
7. 地图显示类型
@property MKMapType mapType; // MKMapTypeStandard, MKMapTypeStatellite, MKMapTypeHybrid;
8. 显示当前位置
@property BOOL showsUserLocation;
@property (readonly) BOOL isUserLocationVisible;
@property (readonly) MKUserLocation *userLocation;
9.限制用户操作
BOOL zoomEnabled;
BOOL scrollEnabled;
BOOL pitchEnabled;
BOOL rotateEnabled;
10. MKMapCamera
设置用户在3D地图上看哪里
+(MKMapCamera*)cameraLookingAtCenterCoordinate:(CLLocationCoordinate2D)coord
fromEyeCoordinate:(CLLocationCoordinate2D)cameraPosition
eyeAltitude:(CLLOcationDistance)eyeAltitude;
11. region
- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated;
// 设置中心
- (void)setCenterCoordinate:(CLLocationCoordinate2D)center animated:(BOOL)animated;
12. point ,rect,coordinate 互转
- (MKMapPoint)mapPointForPoint:(CGPoint)point;
- (MKMapRect)mapRectForRect:(CGRect)rect;
- (CGPoint)pointForMapPoint:(MKMapPoint)mapPoint;
- (CGRect)rectForMapRect:(MKMapRect)mapRect;
13. 地图发生动画,移动,变换摄像头时,被调用:
- (void)mapView:(MKMapView*)mapView didChangeRegionAnimated:(BOOL)animated;
14. MKLocalSearch
code:
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = @"abc";
request.region = ..; // 区域
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){
// response 包含MKMapItem 的数组;
}];
15. MKMapItem : 通过下面方法可以切换到地图app 来显示特点位置
- (BOOL)openInMapsWithLaunchOptions:(NSDictionary*)options;
16. MKPlacemark
包含位置,名称,邮编,等信息
17. MKDirections
通过设置2个MKMapItem,返回一条路线MKPolyline
18. Overlays 在MKMapView上绘制图形
- (void)addOverlay:(id<MKOverlay>)overlay level:(MKOverlayLevel)level;
19. 绘制Overlays时,需要实现MKMapView 的一个delegate:
- (MKOverlayRenderer*)mapView:(MKMapView*)sender
rendererForOverlay:(id<MKOverlay>)overlay;
// 返回一个render
包含:MKCircleRenderer; MKPolylineRenderer; MKPolygonRenderer; MKTileOverlayRenderer;
20. MKShape
二. Embed Segues
1. 将view放到另一个view里;
2. 注:嵌入VC的outlets 没有设置好前,prepareForSegue:sender 就被调用了。
三. Demo - 很不错,需要理解!