标签:
//清空地图
- (void)clearMapView
{ self.mapView.showsUserLocation = NO;//不允许显示用户位置
[self.mapView removeAnnotations:self.mapView.annotations];//清除标记
[self.mapView removeOverlays:self.mapView.overlays];//清除遮罩
self.mapView.delegate = nil;//清除代理}
//清空代理
- (void)clearSearch
{ self.search.delegate = nil;}
/**
* hook,子类覆盖它,实现想要在viewDidAppear中执行一次的方法,搜索中有用到
*/
- (void)hookAction
{}
#pragma mark - Handle Action
//返回时执行清空操作
- (void)returnAction
{ [self.navigationController popViewControllerAnimated:YES];
[self clearMapView];
[self clearSearch];}
#pragma mark - AMapSearchDelegate //搜索代理
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error
{ NSLog(@"%s: searchRequest = %@, errInfo= %@", __func__, [request class], error);}
#pragma mark - Initialization 初始化
//初始化地图
- (void)initMapView
{ self.mapView.frame = self.view.bounds;//给定父视图大小
self.mapView.delegate = self;//设置代理
[self.view addSubview:self.mapView];}
//初始化搜索
- (void)initSearch
{ self.search.delegate = self;}
//初始化导航控制器左按钮
- (void)initBaseNavigationBar
{ self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(returnAction)];
}
//初始化标题
- (void)initTitle:(NSString *)title
{ UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = title;
[titleLabel sizeToFit];
self.navigationItem.titleView = titleLabel;//替换标题视图
}
#pragma mark - Handle URL Scheme
//取得应用名称
- (NSString *)getApplicationName
{
NSDictionary *bundleInfo = [[NSBundle mainBundle] infoDictionary];
return [bundleInfo valueForKey:@"CFBundleDisplayName"] ?: [bundleInfo valueForKey:@"CFBundleName"];
}
//取得应用URLScheme
- (NSString *)getApplicationScheme
{
NSDictionary *bundleInfo = [[NSBundle mainBundle] infoDictionary];
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
NSArray *URLTypes = [bundleInfo valueForKey:@"CFBundleURLTypes"];
NSString *scheme;
for (NSDictionary *dic in URLTypes)
{
NSString *URLName = [dic valueForKey:@"CFBundleURLName"];
if ([URLName isEqualToString:bundleIdentifier])
{
scheme = [[dic valueForKey:@"CFBundleURLSchemes"] objectAtIndex:0];//数组形式
break;
}
}
return scheme;
}
#pragma mark - Life Cycle 生命周期
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (_isFirstAppear)
{
_isFirstAppear = NO;
[self hookAction];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
_isFirstAppear = YES;
[self initTitle:self.title];
[self initBaseNavigationBar];
[self initMapView];
[self initSearch];
}
- (void)dealloc
{
self.mapView.visibleMapRect = MAMapRectMake(220880104, 101476980, 272496, 466656);
}}
2.//根据anntation生成对应的View
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
static NSString *customReuseIndetifier = @"customReuseIndetifier";
CusAnnotationView *annotationView = (CusAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier];//cusAnootationView类型
if (annotationView == nil)
{
annotationView = [[CusAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:customReuseIndetifier];
}
annotationView.canShowCallout = NO;
annotationView.draggable = YES;
annotationView.calloutOffset = CGPointMake(0, -5);
annotationView.portrait = [UIImage imageNamed:@"hema.png"];//设置图片
annotationView.name = @"河马";
return annotationView;
}
return nil;
}
//.标注view的accessory view(必须继承自UIControl)被点击时,触发该回调
- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"accessory view :%@", view);
}
//拖动annotation view时view的状态变化
- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view didChangeDragState:(MAAnnotationViewDragState)newState fromOldState:(MAAnnotationViewDragState)oldState
{
NSLog(@"old :%ld - new :%ld", (long)oldState, (long)newState);
}
//标注view的calloutview整体点击时,触发改回调
- (void)mapView:(MAMapView *)mapView didAnnotationViewCalloutTapped:(MAAnnotationView *)view
{
NSLog(@"callout view :%@", view);
}
#pragma mark MAHeatMapTileOverlay
//根据overlay生成对应的View,MATileOverlayView类型
- (MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id <MAOverlay>)overlay
{
if ([overlay isKindOfClass:[MATileOverlay class]])
{
MATileOverlayView *tileOverlayView = [[MATileOverlayView alloc] initWithTileOverlay:overlay];
return tileOverlayView;
}
return nil;
}
//设置遮罩的透明度
- (void)opacityAction:(UISlider *)slider
{
[self.heatMapTileOverlay setOpacity:slider.value];//改变透明度
MATileOverlayView *render = (MATileOverlayView *)[self.mapView viewForOverlay:self.heatMapTileOverlay];//取出该视图
[render reloadData];//重新加载
}
//设置遮罩半径
- (void)radiusAction:(UISlider *)slider
{
[self.heatMapTileOverlay setRadius:slider.value *100];
MATileOverlayView *render = (MATileOverlayView *)[self.mapView viewForOverlay:self.heatMapTileOverlay];
[render reloadData];
}
//加载热力图节点数据
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.heatMapTileOverlay = [[MAHeatMapTileOverlay alloc] init];//初始化
NSMutableArray* data = [NSMutableArray array];
NSData *jsdata = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heatMapData" ofType:@"json"]];//加载经纬度作为热力图数据!!!!
@autoreleasepool {
if (jsdata)
{
NSArray *dicArray = [NSJSONSerialization JSONObjectWithData:jsdata options:NSJSONReadingAllowFragments error:nil];
for (NSDictionary *dic in dicArray)
{
MAHeatMapNode *node = [[MAHeatMapNode alloc] init];//热力图节点MAHeatMapNode
CLLocationCoordinate2D coordinate;
coordinate.latitude = [dic[@"lat"] doubleValue];
coordinate.longitude = [dic[@"lng"] doubleValue];
node.coordinate = coordinate;//需要一个CLLocationCoordinate2D结构体
node.intensity = [dic[@"count"] doubleValue];//显示的强度
[data addObject:node];
}
}
}
self.heatMapTileOverlay.data = data;//赋值热力点数据-数组
[self.mapView addOverlay:self.heatMapTileOverlay];//添加图层即可
}
4.显示交通状况
self.mapView.showTraffic = YES;//显示交通状况
地图类型
self.mapView.mapType = MAMapTypeStandard;//标准类型 MAMapTypeStandard = 0, // 普通地图 MAMapTypeSatellite, // 卫星地图 MAMapTypeStandardNight // 夜间视图
self.mapView.scrollEnabled = YES;//允许滚动
self.mapView.zoomEnabled = YES;//允许缩放
self.mapView.rotateEnabled = YES;//允许旋转
self.mapView.rotateCameraEnabled = YES;//允许视角
self.animatedCarAnnotation = [[AnimatedAnnotation alloc] initWithCoordinate:coordinate];//经纬度
self.animatedCarAnnotation.animatedImages = carImages;//动画图片数组
self.animatedCarAnnotation.title = @"AutoNavi";//标题
self.animatedCarAnnotation.subtitle = [NSString stringWithFormat:@"Car: %ld images",(unsigned long)[self.animatedCarAnnotation.animatedImages count]];
[self.mapView addAnnotation:self.animatedCarAnnotation];
5.用户定位
//地图跟随模式代理
- (void)mapView:(MAMapView *)mapView didChangeUserTrackingMode:(MAUserTrackingMode)mode animated:(BOOL)animated
self.mapView.userTrackingMode = sender.selectedSegmentIndex;//跟随模式
self.mapView.showsUserLocation = !sender.selectedSegmentIndex;//显示用户位置
[self.mapView addObserver:self forKeyPath:@"showsUserLocation" options:NSKeyValueObservingOptionNew context:nil];//添加键值观察
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"showsUserLocation"])
{
NSNumber *showsNum = [change objectForKey:NSKeyValueChangeNewKey];
self.showSegment.selectedSegmentIndex = ![showsNum boolValue];//是否显示
}
}
/* 生成 地图中心点的 CAKeyframeAnimation. */
- (CAAnimation *)centerMapPointAnimation
{
MAMapPoint fromMapPoint = MAMapPointForCoordinate(CLLocationCoordinate2DMake(39.989870, 116.480940));
MAMapPoint toMapPoint = MAMapPointForCoordinate(CLLocationCoordinate2DMake(31.232992, 121.476773));
#define RATIO 100.f
MAMapSize mapSize = MAMapSizeMake((toMapPoint.x - fromMapPoint.x) / RATIO, (toMapPoint.y - fromMapPoint.y) / RATIO);
CAKeyframeAnimation *centerAnimation = [CAKeyframeAnimation animationWithKeyPath:kMAMapLayerCenterMapPointKey];
centerAnimation.delegate = self;
centerAnimation.duration = kMapCoreAnimationDuration;
centerAnimation.values = [NSArray arrayWithObjects:
[NSValue valueWithMAMapPoint:fromMapPoint],
[NSValue valueWithMAMapPoint:MAMapPointMake(fromMapPoint.x + mapSize.width, fromMapPoint.y + mapSize.height)],
[NSValue valueWithMAMapPoint:MAMapPointMake(toMapPoint.x - mapSize.width, toMapPoint.y - mapSize.height)],
[NSValue valueWithMAMapPoint:toMapPoint],
nil];
centerAnimation.timingFunctions = [NSArray arrayWithObjects:
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],
nil];
centerAnimation.keyTimes = @[@(0.f), @(0.4f), @(0.6f), @(1.f)];
return centerAnimation;
}
/* 生成 地图缩放级别的 CAKeyframeAnimation. */
- (CAAnimation *)zoomLevelAnimation
{
CAKeyframeAnimation *zoomLevelAnimation = [CAKeyframeAnimation animationWithKeyPath:kMAMapLayerZoomLevelKey];
zoomLevelAnimation.delegate = self;
zoomLevelAnimation.duration = kMapCoreAnimationDuration;
zoomLevelAnimation.values = @[@(18), @(5), @(5), @(18)];
zoomLevelAnimation.keyTimes = @[@(0.f), @(0.4f), @(0.6f), @(1.f)];
zoomLevelAnimation.timingFunctions = [NSArray arrayWithObjects:
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],
nil];
return zoomLevelAnimation;
}
/* 生成 地图摄像机俯视角度的 CABasicAnimation. */
- (CAAnimation *)cameraDegreeAnimation
{
CABasicAnimation *cameraDegreeAnimation = [CABasicAnimation animationWithKeyPath:kMAMapLayerCameraDegreeKey];
cameraDegreeAnimation.delegate = self;
cameraDegreeAnimation.duration = kMapCoreAnimationDuration;
cameraDegreeAnimation.fromValue = @(0.f);
cameraDegreeAnimation.toValue = @(45.f);
return cameraDegreeAnimation;
}
/*生成 地图旋转角度的 CABasicAnimation. */
-(CAAnimation *)rotationDegreeKey
{
CABasicAnimation *rotationDegreeAnimation =[CABasicAnimation animationWithKeyPath:kMAMapLayerRotationDegreeKey];
rotationDegreeAnimation.delegate = self;
rotationDegreeAnimation.duration = kMapCoreAnimationDuration;
rotationDegreeAnimation.fromValue = @(0.f);
rotationDegreeAnimation.toValue = @(180.f);
return rotationDegreeAnimation;
}
/* 执行动画. */
- (void)executeCoreAnimation
{
self.mapView.mapType = MAMapTypeSatellite;
/* 添加 中心点 动画. */
[self.mapView.layer addAnimation:[self centerMapPointAnimation] forKey:kMAMapLayerCenterMapPointKey];
/* 添加 缩放级别 动画. */
[self.mapView.layer addAnimation:[self zoomLevelAnimation] forKey:kMAMapLayerZoomLevelKey];
/* 添加 摄像机俯视角度 动画. */
[self.mapView.layer addAnimation:[self cameraDegreeAnimation] forKey:kMAMapLayerCameraDegreeKey];
/* 添加 地图旋转角度 动画. */
[self.mapView.layer addAnimation:[self rotationDegreeKey] forKey:kMAMapLayerRotationDegreeKey];
}
/// 驾车:0-速度最快(时间); 1-避免收费(不走收费路段的最快道路); 2-距离优先; 3-不走高速; 4-结合实时交通(躲避拥堵); 5-不走高速且避免收费; 6-不走高速且躲避拥堵; 7-躲避收费和拥堵; 8-不走高速且躲避收费和拥堵
/// 公交:0-最快捷; 1-最经济; 2-最少换乘; 3-最少步行; 4-最舒适; 5-不乘地铁;
/// 步行,无策略,均一样
- (void)shareAction
{
AMapRouteShareSearchRequest *request = [[AMapRouteShareSearchRequest alloc] init];
request.startCoordinate = [AMapGeoPoint locationWithLatitude:self.startCoordinate.latitude longitude:self.startCoordinate.longitude];
request.destinationCoordinate = [AMapGeoPoint locationWithLatitude:self.destinationCoordinate.latitude longitude:self.destinationCoordinate.longitude];
request.startName = kStartTitle;
request.destinationName = kEndTitle;
request.type = self.shareRouteType;
request.strategy = 0;
[self.search AMapRouteShareSearch:request];
}
附近搜索
AMapNearbySearchRequest *request = [[AMapNearbySearchRequest alloc] init];
request.center = [AMapGeoPoint locationWithLatitude:39.001 longitude:114.002];//附近搜索的请求
[self.search AMapNearbySearch:request];
- (AMapNearbyUploadInfo *)nearbyInfoForUploading:(AMapNearbySearchManager *)manager
- (void)onUserInfoClearedWithError:(NSError *)error
- (void)onNearbyInfoUploadedWithError:(NSError *)error
- (void)onNearbySearchDone:(AMapNearbySearchRequest *)request response:(AMapNearbySearchResponse *)response
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
标签:
原文地址:http://www.cnblogs.com/shaohui/p/5334288.html