//清空地圖node
- (void)clearMapViewgit
{ self.mapView.showsUserLocation = NO;//不容許顯示用戶位置json
[self.mapView removeAnnotations:self.mapView.annotations];//清除標記數組
[self.mapView removeOverlays:self.mapView.overlays];//清除遮罩app
self.mapView.delegate = nil;//清除代理}ide
//清空代理動畫
- (void)clearSearch代理
{ self.search.delegate = nil;}orm
/**server
* 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