//.h#import//地图大头针
@class MZAnnotation;
@interface MZMapView : MKMapView
//地图
+ (MKMapView *)addSubview:(UIView *)view;
+ (MKMapView *)addSubview:(UIView *)view frame:(CGRect)frame;
+ (MKMapView *)addSubview:(UIView *)view mapType:(MKMapType)mapType;
+ (MKMapView *)addSubview:(UIView *)view mapType:(MKMapType)mapType frame:(CGRect)frame;
//搜索
+ (void)searchFor:(NSString *)search result:(void(^)(NSArray*placemarks,NSError *error))result;
//定位
+ (void)positionWith:(MKMapView *)mapView coordinate:(CLLocationCoordinate2D)coordinate span:(MKCoordinateSpan)span;
+ (void)positionWith:(MKMapView *)mapView coordinate:(CLLocationCoordinate2D)coordinate latitudeDelta:(CGFloat)latitudeDelta longitudeDelta:(CGFloat)longitudeDelta;
+ (void)positionWith:(MKMapView *)mapView mark:(CLPlacemark *)mark span:(MKCoordinateSpan)span;
+ (void)positionWith:(MKMapView *)mapView mark:(CLPlacemark *)mark latitudeDelta:(CGFloat)latitudeDelta longitudeDelta:(CGFloat)longitudeDelta;
+ (void)positionWith:(MKMapView *)mapView markArray:(NSArray*)markArray span:(MKCoordinateSpan)span;
+ (void)positionWith:(MKMapView *)mapView markArray:(NSArray*)markArray latitudeDelta:(CGFloat)latitudeDelta longitudeDelta:(CGFloat)longitudeDelta;
//添加大头针
+ (void)addAnnotationWith:(MKMapView *)mapView annotation:(MZAnnotation *)annotation;
+ (void)addAnnotationWith:(MKMapView *)mapView title:(NSString *)title subTitle:(NSString *)subTitle coordination:(CLLocationCoordinate2D)coordinate;
+ (void)addAnnotationWith:(MKMapView *)mapView annotationForMark:(CLPlacemark *)mark;
@end
@interface MZAnnotation : NSObject
@property (nonatomic,copy)NSString *title;
@property (nonatomic,copy)NSString *subTitle;
@property (nonatomic,assign)CLLocationCoordinate2D coordinate;
@end
//.m
#import "MZMapView.h"
@implementation MZMapView
+ (MKMapView *)addSubview:(UIView *)view {
return [self addSubview:view mapType:MKMapTypeStandard frame:CGRectZero];
}
+ (MKMapView *)addSubview:(UIView *)view frame:(CGRect)frame {
return [self addSubview:view mapType:MKMapTypeStandard frame:frame];
}
+ (MKMapView *)addSubview:(UIView *)view mapType:(MKMapType)mapType {
return [self addSubview:view mapType:mapType frame:CGRectZero];
}
+ (MKMapView *)addSubview:(UIView *)view mapType:(MKMapType)mapType frame:(CGRect)frame {
MKMapView *mzMapView = [[MKMapView alloc]initWithFrame:frame];
mzMapView.mapType = mapType;
return mzMapView;
}
//搜索
+ (void)searchFor:(NSString *)search result:(void(^)(NSArray*placemarks,NSError *error))result { CLGeocoder *geo=[[CLGeocoder alloc]init]; [geo geocodeAddressString:search completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {
if (result) {
result(placemarks,error);
}
}];
}
//定位
+ (void)positionWith:(MKMapView *)mapView coordinate:(CLLocationCoordinate2D)coordinate span:(MKCoordinateSpan)span {
MKCoordinateRegion region=MKCoordinateRegionMake(coordinate, span);
[mapView setRegion:region animated:YES];
}
+ (void)positionWith:(MKMapView *)mapView coordinate:(CLLocationCoordinate2D)coordinate latitudeDelta:(CGFloat)latitudeDelta longitudeDelta:(CGFloat)longitudeDelta {
MKCoordinateSpan span=MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
MKCoordinateRegion region=MKCoordinateRegionMake(coordinate, span);
[mapView setRegion:region animated:YES];
}
+ (void)positionWith:(MKMapView *)mapView mark:(CLPlacemark *)mark span:(MKCoordinateSpan)span {
[self positionWith:mapView coordinate:mark.location.coordinate span:span];
}
+ (void)positionWith:(MKMapView *)mapView mark:(CLPlacemark *)mark latitudeDelta:(CGFloat)latitudeDelta longitudeDelta:(CGFloat)longitudeDelta {
[self positionWith:mapView coordinate:mark.location.coordinate latitudeDelta:latitudeDelta longitudeDelta:longitudeDelta];
}
+ (void)positionWith:(MKMapView *)mapView markArray:(NSArray*)markArray span:(MKCoordinateSpan)span {
for (CLPlacemark *mark in markArray) {
[self positionWith:mapView coordinate:mark.location.coordinate span:span];
}
}
+ (void)positionWith:(MKMapView *)mapView markArray:(NSArray*)markArray latitudeDelta:(CGFloat)latitudeDelta longitudeDelta:(CGFloat)longitudeDelta {
for (CLPlacemark *mark in markArray) {
[self positionWith:mapView coordinate:mark.location.coordinate latitudeDelta:latitudeDelta longitudeDelta:longitudeDelta];
}
}
//添加大头针
+ (void)addAnnotationWith:(MKMapView *)mapView annotation:(MZAnnotation *)annotation {
[mapView addAnnotation:annotation];
}
+ (void)addAnnotationWith:(MKMapView *)mapView title:(NSString *)title subTitle:(NSString *)subTitle coordination:(CLLocationCoordinate2D)coordinate {
MZAnnotation *annotation = [[MZAnnotation alloc]init];
annotation.title = title;
annotation.subTitle = subTitle;
annotation.coordinate = coordinate;
[mapView addAnnotation:annotation];
}
+ (void)addAnnotationWith:(MKMapView *)mapView annotationForMark:(CLPlacemark *)mark {
MZAnnotation *annotation=[[MZAnnotation alloc]init];
annotation.title=mark.name;
annotation.coordinate=mark.location.coordinate;
[mapView addAnnotation:annotation];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
@implementation MZAnnotation
@end
谢谢阅读!
网友评论