【iOS~ 高德地圖:2、示例:臺風運行軌跡 + 自定義氣泡標注:】
【iOS~ 高德地圖:3、自定義氣泡標注】
圖片覆蓋物
:【氣溫】、【降水】
點平滑移動
:【臺風】
點標注+氣泡標注
:【自定義氣泡標注】
圖片覆蓋物,使用NSTimer
,時間軸(時間刻度)循環更改圖片覆蓋物,原理:刪除,置為nil,在重新創建
if (self.groundOverlay != nil) {
[self.mapView removeOverlay:self.groundOverlay]; // 點擊臺風的時候,刪掉覆蓋物層,在需要的時候重新創建
self.groundOverlay = nil;
}
正式代碼:ViewController.m:
/** 高德地圖 */
#import <AMapFoundationKit/AMapFoundationKit.h> // 高德地圖 需要引入AMapFoundationKit.h頭文件
#import <MAMapKit/MAMapKit.h> // 3D矢量圖
#import <AMapLocationKit/AMapLocationKit.h> // 實現持續定位
/// tabBar雷達 信息:氣溫+降水(圖片)
#import "GWHomeRaderMap_DataModel.h"
/// 點擊地圖之后,顯示該坐標的天氣信息(自定義氣泡標注)
#import "GWHomeRaderMap_LocationWeatherModel.h"
#import "GWRaderMap_CustomAnnotationView.h"http:// 自定義氣泡view
@interface GWHomeRaderMapCTRL () <MAMapViewDelegate, AMapLocationManagerDelegate>
@property (nonatomic, strong) UIButton *tempBut; // 氣溫
@property (nonatomic, strong) UIButton *aqiBut; // 空氣(暫時 無)
@property (nonatomic, strong) UIButton *rainBut; // 降水
@property (nonatomic, strong) UIButton *typhoonBut;// 臺風
/** 1、高德地圖 */
@property (nonatomic, strong) UIView *mapBackView;
@property (nonatomic, strong) MAMapView *mapView;
// 定位
@property (nonatomic, strong) AMapLocationManager *locationManager;
// 【圖片覆蓋物】
@property (nonatomic, strong) MAGroundOverlay *groundOverlay;
@property (nonatomic, assign) MACoordinateBounds coordinateBounds; // 東北、西南兩個點定義的四邊形經緯度范圍
/** 2、自定義:功能 */
/// 氣候等級
@property (nonatomic, strong) UIView *climateRank_BackView;
@property (nonatomic, strong) UIImageView *climateRank_Img;
@property (nonatomic, strong) UILabel *climateRank_minTipsL;
@property (nonatomic, strong) UILabel *climateRank_maxTipsL;
/// 當前位置
@property (nonatomic, strong) UIButton *currentPositionBut;
/** 3、播放/暫停:氣候變化,(可以 手動滑動 刻度) */
@property (nonatomic, strong) UIView *progress_BackView; // 寬度:290
@property (nonatomic, strong) UIButton *progress_playBut;
@property (nonatomic, strong) UILabel *progress_beginTimeL;
@property (nonatomic, strong) UILabel *progress_endTimeL;
@property (nonatomic, strong) UIView *progress_lineBackView; // 刻度的父view,寬度:(290 - 40 -25) = 225
@property (nonatomic, strong) UIView *progress_currentLineView; // 當前刻度view
@property (nonatomic, strong) UILabel *progress_tipsL; // 日期 時間:“08-10 12:00”
@property (nonatomic, strong) UIView *progress_view; // 覆蓋在最上邊的一個view(// Touch觸摸 view視圖)
/** 4、臺風:經度、緯度、風力、最大風速、中心氣壓、距離本地 */
@property (nonatomic, strong) UIView *typhoon_BackView;
@property (nonatomic, strong) UILabel *noTyphoon_tipsL;
@property (nonatomic, strong) UILabel *typhoon_longitudeTipsL;
@property (nonatomic, strong) UILabel *typhoon_longitudeL;
@property (nonatomic, strong) UILabel *typhoon_latitudeTipsL;
@property (nonatomic, strong) UILabel *typhoon_latitudeL;
@property (nonatomic, strong) UILabel *typhoon_windTipsL;
@property (nonatomic, strong) UILabel *typhoon_windL;
@property (nonatomic, strong) UILabel *typhoon_maxWindSpeedTipsL;
@property (nonatomic, strong) UILabel *typhoon_maxWindSpeedL;
@property (nonatomic, strong) UILabel *typhoon_pressureTipsL;
@property (nonatomic, strong) UILabel *typhoon_pressureL;
@property (nonatomic, strong) UILabel *typhoon_distanceTipsL;
@property (nonatomic, strong) UILabel *typhoon_distanceL;
// 【繪制折線 覆蓋物】
@property (nonatomic, strong) MAPolyline *commonPolyline;
@property (nonatomic, strong) MAAnimatedAnnotation *typhoon_AnimatedAnnotation; // 臺風動畫
//@property (nonatomic, strong) MAAnnotationView *typhoon_AnnotationView; // 臺風標注view
//@property (nonatomic, strong) MAPointAnnotation *typhoon_PointAnnotation;
@property (nonatomic, strong) MAAnimatedAnnotation *locatinWeather_AnimatedAnnotation; // 點擊地圖,彈出自定義天氣view
/** 雷達數據 */
@property (nonatomic, strong) GWHomeRaderMap_DataModel *raderMapDateModel;
@property (nonatomic, strong) GWHomeRaderMap_LocationWeatherModel *locationWeatherModel;
@property (nonatomic, strong) NSArray *tempImageArray; // 氣溫圖片數組
@property (nonatomic, strong) NSArray *rainImageArray; // 降水圖片數組
@property (nonatomic, strong) NSArray *currentImageArray; // 當前的圖片數組
@property (nonatomic, assign) NSInteger itemCell; // 第幾個圖片的索引值
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation GWHomeRaderMapCTRL
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = RGBA(248, 246, 242, 1);
// self.navigationBar.hidden = YES;
self.statusBg.hidden = YES;
self.leftButton.hidden = YES;
self.rightButton.hidden = YES;
self.navigationBar.backgroundColor = [UIColor clearColor];
// 用戶授權高德SDK隱私協議狀態(必須在AMapLocationManager實例化之前調用)
[AMapLocationManager updatePrivacyAgree:AMapPrivacyAgreeStatusDidAgree];
[AMapLocationManager updatePrivacyShow:AMapPrivacyShowStatusDidShow privacyInfo:AMapPrivacyInfoStatusDidContain];
[self setupUI];
[self setMapUI];
[self settingMapLocation];
[self setupTempAndRainOtherFunction];
[self setupTyphoonOtherFunction];
[self gainTempAndRainImages_Request]; // 請求接口時,地圖滑不動,(后期解決一下)
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
if (_timer) {
[_timer invalidate];
_timer = nil;
}
self.tempBut.selected = YES;
self.rainBut.selected = NO;
self.typhoonBut.selected = NO;
self.aqiBut.selected = NO;
self.climateRank_BackView.hidden = NO;
self.climateRank_Img.image = [UIImage imageNamed:@"raderMap_TempRankColor_icon"];
self.climateRank_minTipsL.text = @"-40°";
self.climateRank_maxTipsL.text = @"40°";
self.progress_playBut.selected = NO;
self.typhoon_BackView.hidden = YES;
if (self.tempImageArray.count > 0) {
[self showTemp_Action];
}
if (self.raderMapDateModel == nil) {
[self gainTempAndRainImages_Request];
}
// 判斷 ??定位 是否開啟(自定義方法)
[[LCM_Tool shareInstance] gainCurrentLoaction_latitudeAndLongitude];
// 在未開啟定位之前,可以將用戶位置為中心,或設置一個默認位置:
if (self.mapView.userLocation.location != nil) {
[self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate animated:NO];
} else {
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(39.909164, 116.397643) animated:NO];
}
//(高德)調用AMapLocationManager提供的startUpdatingLocation方法開啟持續定位。(調用此方法會cancel掉所有的單次定位請求。)
// [self startSerialLocation];
//(高德)單次定位:
[self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
if (error == nil) {
NSLog(@"???? 單次定位");
}
NSLog(@"location:{緯度lat:%f; 經度lon:%f; 精確度accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
if (regeocode)
{
NSLog(@"reGeocode:%@", regeocode);
}
// self.mapView.centerCoordinate = location.coordinate;
// [self.mapView setCenterCoordinate:location.coordinate animated:YES];
// [self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate animated:YES];
/**
regeocode = AMapLocationReGeocode:{formattedAddress:北京市朝陽區東三環購物中心; country:中國;province:北京市; city:北京市; district:朝陽區; citycode:010; adcode:110105; street:東三環; number:***大廈1層; POIName:***大廈購物中心; AOIName:***大廈購物中心;}
*/
}];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
//(高德)取消持續定位 (和 單次定位)
[self stopSerialLocation];
}
#pragma mark -- ---- 點擊 navigation 換不同的氣候圖像 ---- --
- (void)clickRaderMapBotton:(UIButton *)titleBut {
if (self.currentImageArray.count <= 0 || (self.itemCell >= self.currentImageArray.count && self.currentImageArray.count > 0)) {
return;
}
self.climateRank_BackView.hidden = NO;
self.currentPositionBut.hidden = NO;
self.progress_BackView.hidden = NO;
self.progress_tipsL.hidden = NO;
self.progress_currentLineView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(40-1), 0, [UIScreen mainScreen].bounds.size.width/375*2, [UIScreen mainScreen].bounds.size.width/375*22);
self.typhoon_BackView.hidden = YES;
if (titleBut == self.tempBut) {
self.tempBut.selected = YES;
self.rainBut.selected = NO;
self.typhoonBut.selected = NO;
self.aqiBut.selected = NO;
self.climateRank_Img.image = [UIImage imageNamed:@"raderMap_TempRankColor_icon"];
self.climateRank_minTipsL.text = @"-40°";
self.climateRank_maxTipsL.text = @"40°";
[self showTemp_Action];
[self gain_ClickMap_Request_WithLocationCoordinate:self.mapView.userLocation.location.coordinate];
} else if (titleBut == self.rainBut) {
self.tempBut.selected = NO;
self.rainBut.selected = YES;
self.typhoonBut.selected = NO;
self.aqiBut.selected = NO;
self.climateRank_Img.image = [UIImage imageNamed:@"raderMap_RainRankColor_icon"];
self.climateRank_minTipsL.text = @"小";
self.climateRank_maxTipsL.text = @"大";
[self showRain_Action];
[self gain_ClickMap_Request_WithLocationCoordinate:self.mapView.userLocation.location.coordinate];
} else if (titleBut == self.typhoonBut) {
self.tempBut.selected = NO;
self.rainBut.selected = NO;
self.typhoonBut.selected = YES;
self.aqiBut.selected = NO;
self.climateRank_BackView.hidden = YES;
self.currentPositionBut.hidden = YES;
self.progress_BackView.hidden = YES;
self.progress_tipsL.hidden = YES;
self.typhoon_BackView.hidden = NO;
if (self.raderMapDateModel.typhoon.count <= 0) { // 無臺風
self.typhoon_longitudeTipsL.hidden = YES;
self.typhoon_longitudeL.hidden = YES;
self.typhoon_latitudeTipsL.hidden = YES;
self.typhoon_latitudeL.hidden = YES;
self.typhoon_maxWindSpeedTipsL.hidden = YES;
self.typhoon_maxWindSpeedL.hidden = YES;
self.typhoon_pressureTipsL.hidden = YES;
self.typhoon_pressureL.hidden = YES;
self.noTyphoon_tipsL.hidden = NO;
self.typhoon_BackView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*20, [UIScreen mainScreen].bounds.size.width/375*14 + k_Height_StatusBar+k_Height_NavContentBar, [UIScreen mainScreen].bounds.size.width/375*96, [UIScreen mainScreen].bounds.size.width/375*28);
} else { // 有臺風(有數據)
self.typhoon_BackView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*20, [UIScreen mainScreen].bounds.size.width/375*14 + k_Height_StatusBar+k_Height_NavContentBar, [UIScreen mainScreen].bounds.size.width/375*150, [UIScreen mainScreen].bounds.size.width/375*110);
self.typhoon_longitudeTipsL.hidden = NO;
self.typhoon_longitudeL.hidden = NO;
self.typhoon_latitudeTipsL.hidden = NO;
self.typhoon_latitudeL.hidden = NO;
self.typhoon_maxWindSpeedTipsL.hidden = NO;
self.typhoon_maxWindSpeedL.hidden = NO;
self.typhoon_pressureTipsL.hidden = NO;
self.typhoon_pressureL.hidden = NO;
self.noTyphoon_tipsL.hidden = YES;
if (self.raderMapDateModel.typhoon.count > 0 && self.raderMapDateModel.typhoon[0].routes.count > 0) {
GWHomeRaderMap_TyphoonRoutesModel *routesModel = self.raderMapDateModel.typhoon[0].routes[self.raderMapDateModel.typhoon[0].routes.count-1];
self.typhoon_longitudeL.text = [NSString stringWithFormat:@"%.f° E", routesModel.longitude];
self.typhoon_latitudeL.text = [NSString stringWithFormat:@"%.f° N", routesModel.latitude];
self.typhoon_maxWindSpeedL.text = [NSString stringWithFormat:@"%ld 米/秒", routesModel.maxWind];
self.typhoon_pressureL.text = [NSString stringWithFormat:@"%ld 百帕", routesModel.pressure];
}
}
if (self.groundOverlay != nil) {
[self.mapView removeOverlay:self.groundOverlay]; // 點擊臺風的時候,刪掉覆蓋物層,在需要的時候重新創建
self.groundOverlay = nil;
}
if (_timer) {
[_timer invalidate]; // 停止,從Runlop中移除
_timer = nil; // 放在內存溢出,重置為nil
}
/** 繪制臺風軌跡: */
[self setupTyphoonTrack];
} else if (titleBut == self.aqiBut) {
}
}
- (void)click_backCurrentPositionAction {
// 回到(用戶)當前位置
// self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;
[self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate animated:YES];
[self gain_ClickMap_Request_WithLocationCoordinate:self.mapView.userLocation.location.coordinate];
}
- (void)click_playClimateChangeAction {
// 氣候變化(播放特效)
if (self.currentImageArray.count <= 0 || self.itemCell >= self.currentImageArray.count) {
return;
}
self.progress_playBut.selected = !self.progress_playBut.selected;
if (self.progress_playBut.selected) { // 循環播放特效
[self.timer setFireDate:[NSDate date]]; // 繼續
} else { // 暫停
[self.timer setFireDate:[NSDate distantFuture]]; // 暫停
UIImage *currentImage = self.currentImageArray[self.itemCell];
if (self.groundOverlay != nil) {
[self.mapView removeOverlay:self.groundOverlay]; // 點擊臺風的時候,刪掉覆蓋物層,在需要的時候重新創建
self.groundOverlay = nil;
}
// 根據bounds值和icon生成GroundOverlay
self.groundOverlay = [MAGroundOverlay groundOverlayWithBounds:self.coordinateBounds icon:currentImage];
// self.mapView.visibleMapRect = self.groundOverlay.boundingMapRect; // 可見區域, 設定的該范圍可能會被調整為適合地圖窗口顯示的范圍
[self.mapView addOverlay:self.groundOverlay];
if (self.tempBut.selected) {
} else if (self.rainBut.selected) {
}
}
}
#pragma mark -- 1、設置 地圖 --
- (void)setMapUI {
_mapBackView = [[UIView alloc] initWithFrame:CGRectMake(0, k_Height_StatusBar + k_Height_NavContentBar, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - (k_Height_StatusBar + k_Height_NavContentBar + k_Height_TabBar))];
[self.view addSubview:self.mapBackView];
self.mapBackView.backgroundColor = RGBA(248, 246, 242, 1);
///是否打開地形圖,默認NO. 注意:需在地圖創建前設置
// MAMapView.terrainEnabled = YES;
///初始化地圖
_mapView = [[MAMapView alloc] initWithFrame:self.mapBackView.bounds];
///如果您需要進入地圖就顯示定位小藍點,則需要下面兩行代碼
self.mapView.showsUserLocation = YES;
self.mapView.userTrackingMode = MAUserTrackingModeFollow;
self.mapView.delegate = self;
self.mapView.zoomLevel = 18;
self.mapView.rotationDegree = 0.f; ///設置地圖旋轉角度(逆時針為正向)
self.mapView.cameraDegree = 0.f; ///設置地圖相機角度(范圍為[0.f, 60.f],但高于40度的角度需要在16級以上才能生效)
// self.mapView.rotateEnabled = NO; ///是否支持旋轉, 默認YES
self.mapView.rotateCameraEnabled = NO; ///是否支持camera旋轉(傾斜手勢), 默認YES
self.mapView.showsCompass = YES; ///是否顯示指南針, 默認YES
///把地圖添加至view
[self.mapBackView addSubview:self.mapView];
[self.mapView setZoomLevel:12 animated:YES];
// 標注:
// MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
}
#pragma mark -- <MAMapViewDelegate> --
/**
* @brief 單擊地圖回調,返回經緯度
* @param mapView 地圖View
* @param coordinate 經緯度
*/
- (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate {
NSLog(@"???? 點擊地圖的時候,返回的經度 = %.f,\n緯度 = %.f", coordinate.longitude, coordinate.latitude);
// self.mapView.centerCoordinate = coordinate;
// [self.mapView setCenterCoordinate:coordinate animated:YES]; // 設置當前地圖的中心點,改變該值時,地圖的比例尺級別不會發生變化
if (self.tempBut.selected || self.rainBut.selected) {
// 調用請求,獲取該位置的天氣信息
[self gain_ClickMap_Request_WithLocationCoordinate:coordinate];
}
}
/**
* @brief 地圖將要發生移動時調用此接口
* @param mapView 地圖view
* @param wasUserAction 標識是否是用戶動作
*/
- (void)mapView:(MAMapView *)mapView mapWillMoveByUser:(BOOL)wasUserAction {
if (wasUserAction) {
NSLog(@" 地圖將要 移動");
[MobClick event:@"radarpage_slide" label:@"用戶拖動地圖"];
}
}
/**
* @brief 地圖移動結束后調用此接口
* @param mapView 地圖view
* @param wasUserAction 標識是否是用戶動作
*/
- (void)mapView:(MAMapView *)mapView mapDidMoveByUser:(BOOL)wasUserAction {
if (wasUserAction) {
NSLog(@" 地圖結束 移動");
}
}
/**
* @brief 地圖將要發生縮放時調用此接口
* @param mapView 地圖view
* @param wasUserAction 標識是否是用戶動作
*/
- (void)mapView:(MAMapView *)mapView mapWillZoomByUser:(BOOL)wasUserAction {
if (wasUserAction) {
NSLog(@" 地圖將要 縮放");
[MobClick event:@"radarpage_slide" label:@"用戶縮放地圖"];
}
}
/**
* @brief 地圖縮放結束后調用此接口
* @param mapView 地圖view
* @param wasUserAction 標識是否是用戶動作
*/
- (void)mapView:(MAMapView *)mapView mapDidZoomByUser:(BOOL)wasUserAction {
if (wasUserAction) {
NSLog(@" 地圖結束 縮放");
}
}
#pragma mark ========>>>>>>>>>> 方法【mapView:rendererForOverlay:】 <<<<<<<<<<<<==========
/// 實現<MAMapViewDelegate>協議中的mapView:rendererForOverlay:回調函數,以在地圖上顯示圖片覆蓋物。
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay
{
if ([overlay isKindOfClass:[MAGroundOverlay class]]) { // 【氣溫】、【降水】overlay
MAGroundOverlayRenderer *groundOverlayRenderer = [[MAGroundOverlayRenderer alloc] initWithGroundOverlay:overlay];
return groundOverlayRenderer;
} else if ([overlay isKindOfClass:[MAPolyline class]]) { // 【臺風 軌跡線】overlay
MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
polylineRenderer.is3DArrowLine = NO;///設置是否顯示3d箭頭線, 默認為NO。如果設置為YES,則為3d箭頭線。
polylineRenderer.sideColor = RGBA(31, 127, 251, 1);
polylineRenderer.showRangeEnabled = NO; ///是否啟用顯示范圍,YES啟用,不啟用時展示全路徑
polylineRenderer.lineWidth = 3.f; ///筆觸寬度
polylineRenderer.fillColor = UIColor.clearColor; ///填充顏色
polylineRenderer.strokeColor = RGBA(31, 127, 251, 1);///筆觸顏色
polylineRenderer.lineJoinType = kMALineJoinRound;// 接頭
polylineRenderer.lineCapType = kMALineCapArrow; // 線端(帶箭頭)
polylineRenderer.miterLimit = 20.f;
polylineRenderer.lineDashType = kMALineDashTypeNone; /// 虛線樣式(已經走過的路線的畫實線,預測的畫虛線)
return polylineRenderer;
}
return nil;
}
#pragma mark -- 2、持續定位 --
- (void)settingMapLocation {
_locationManager = [[AMapLocationManager alloc] init];
self.locationManager.delegate = self;
// 帶逆地理信息的一次定位(返回坐標和地址信息)
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
// 定位超時時間,最低2s,此處設置為10s
self.locationManager.locationTimeout =10;
// 逆地理請求超時時間,最低2s,此處設置為10s
self.locationManager.reGeocodeTimeout = 10;
}
- (void)startSerialLocation
{
// (持續)開始定位
[self.locationManager startUpdatingLocation];
}
- (void)stopSerialLocation
{
// 停止定位
[self.locationManager stopUpdatingLocation];
}
#pragma mark -- 地位代理方法、回調 <AMapLocationManagerDelegate> ==
/**
* @brief (連續定位回調函數).注意:如果實現了本方法,則定位信息不會通過amapLocationManager:didUpdateLocation:方法回調。
* @param manager 定位 AMapLocationManager 類。
* @param location 定位結果。
* @param reGeocode 逆地理信息。
*/
//接收位置更新,實現AMapLocationManagerDelegate代理的amapLocationManager:didUpdateLocation方法,處理位置更新
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode {
NSLog(@"location:{緯度lat:%f; 經度lon:%f; 精確度accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
if (reGeocode)
{
NSLog(@"reGeocode:%@", reGeocode);
}
}
/**
* @brief 當定位發生錯誤時,會調用代理的此方法。
* @param manager 定位 AMapLocationManager 類。
* @param error 返回的錯誤,參考 CLError 。
*/
- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"???? 報錯:error ====>>>> %@", error);
}
#pragma mark ****** 3、網絡請求:返回多張圖片 (根據東北角、西南角兩個點的經緯度,覆蓋到地圖上) ******
- (void)gainTempAndRainImages_Request {
[BRNetworkHepler getWithUrl:@"https://www.baidu.com.8769/more/images" params:nil headers:nil success:^(GWHttpBaseResponseModel *responseObject, NSString *message) {
NSInteger code = responseObject.code;
if (code == 200) {
GWHomeRaderMap_DataModel *dateModel = [GWHomeRaderMap_DataModel modelWithDictionary:responseObject.data];
// 處理一下時間(增加一個時間,格式:MM-dd HH:mm)
for (int i=0; i < dateModel.temp.list.count; i++) {
GWHomeRaderMap_TempListModel *model = dateModel.temp.list[i];
dateModel.temp.list[i].timeStr = [LCM_Tool TimeStampTransform:model.timestamp andstyle:@"MM-dd HH:mm"];
}
for (int i=0; i < dateModel.precip.list.count; i++) {
GWHomeRaderMap_PrecipListModel *model = dateModel.precip.list[i];
dateModel.precip.list[i].timeStr = [LCM_Tool TimeStampTransform:model.timestamp andstyle:@"MM-dd HH:mm"];
}
self.raderMapDateModel = dateModel;
NSMutableArray *tempImgs = [NSMutableArray arrayWithCapacity:0];
NSMutableArray *rainImgs = [NSMutableArray arrayWithCapacity:0];
// 使用GCD方法,異步加載圖片操作:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 處理耗時操作的代碼塊...
for (GWHomeRaderMap_TempListModel *tempListModel in self.raderMapDateModel.temp.list) {
// 如果要從url同步獲取圖片,可以用如下方式:
// 但是由于這種方式是同步的,如果網速不夠快,它會卡住界面。所以需要使用異步方式。(這里使用GCD)
NSData *tempData = [NSData dataWithContentsOfURL:[NSURL URLWithString:tempListModel.url]];
UIImage *image = [UIImage imageWithData:tempData];
[tempImgs addObject:image];
}
for (GWHomeRaderMap_TempListModel *rainListModel in self.raderMapDateModel.precip.list) {
NSData *rainData = [NSData dataWithContentsOfURL:[NSURL URLWithString:rainListModel.url]];
UIImage *image = [UIImage imageWithData:rainData];
[rainImgs addObject:image];
}
self.tempImageArray = [tempImgs copy];
self.rainImageArray = [rainImgs copy];
self.currentImageArray = [self.tempImageArray copy];
//通知主線程刷新
dispatch_async(dispatch_get_main_queue(), ^{
//回調或者說是通知主線程刷新,(這里可以保存、刪除一下數據)
if (self.mapView) {
[self showTemp_Action];
self.progress_BackView.hidden = NO;
self.progress_tipsL.hidden = NO;
}
});
});
} else {
[LCM_AlertViewFactory showToastWithMessage:responseObject.msg];
}
} failure:^(NSError *error, NSString *message) {
NSLog(@"error =====>>>> %@", error);
NSLog(@"message ---->> %@", message);
}];
}
#pragma mark -- NSTimer: 將圖片 一遍遍的 循環展示(輪詢) --
- (void)showTemp_Action {
// 溫度 圖片
if (self.raderMapDateModel != nil) {
if (_timer) {
[_timer invalidate];
_timer = nil;
}
if (self.groundOverlay != nil) {
[self.mapView removeOverlay:self.groundOverlay]; // 點擊臺風的時候,刪掉覆蓋物層,在需要的時候重新創建
self.groundOverlay = nil;
}
if (self.commonPolyline != nil) {
[self.mapView removeOverlay:self.commonPolyline]; // 刪除 繪制折線overlay
self.commonPolyline = nil;
}
for (MAAnnotationMoveAnimation *animation in [self.typhoon_AnimatedAnnotation allMoveAnimations]) {
[animation cancel];
}
if (self.typhoon_AnimatedAnnotation != nil) {
[self.mapView removeAnnotation:self.typhoon_AnimatedAnnotation]; // 刪除 【點平滑移動】(這里特指 臺風的圖片)overlay
self.typhoon_AnimatedAnnotation = nil;
}
// if (self.typhoon_PointAnnotation != nil) {
// [self.mapView removeAnnotation:self.typhoon_PointAnnotation]; // 刪除 【點標記??】(這里特指 臺風的圖片)overlay
// self.typhoon_PointAnnotation = nil;
// }
if (self.locatinWeather_AnimatedAnnotation != nil) {
[self.mapView removeAnnotation:self.locatinWeather_AnimatedAnnotation]; // 刪除 【自定義氣泡】Annotation
self.locatinWeather_AnimatedAnnotation = nil;
}
self.currentImageArray = [self.tempImageArray copy];
[self create_TempTimeline];
self.progress_playBut.selected = NO;
self.progress_beginTimeL.text = [NSString stringWithFormat:@"%@", self.raderMapDateModel.temp.list[0].timeStr];
self.progress_endTimeL.text = [NSString stringWithFormat:@"%@", self.raderMapDateModel.temp.list[self.raderMapDateModel.temp.list.count-1].timeStr];
self.progress_tipsL.text = self.raderMapDateModel.temp.list[0].timeStr;
self.progress_tipsL.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(15+40 -30), [UIScreen mainScreen].bounds.size.height - k_Height_TabBar - [UIScreen mainScreen].bounds.size.width/375*(19+44+22), [UIScreen mainScreen].bounds.size.width/375*60, [UIScreen mainScreen].bounds.size.width/375*22);
// 東北角、西南角的經緯度
NSArray *areaArray = [self.raderMapDateModel.temp.area componentsSeparatedByString:@","];
CGFloat southLatitude = [areaArray[0] floatValue];
CGFloat southLongitude = [areaArray[1] floatValue];
CGFloat northLatitude = [areaArray[2] floatValue];
CGFloat northLongitude = [areaArray[3] floatValue];
// 【氣溫】圖片覆蓋物:
// 前:東北角,后:西南角
MACoordinateBounds coordinateBounds = MACoordinateBoundsMake(CLLocationCoordinate2DMake(northLatitude, northLongitude),CLLocationCoordinate2DMake(southLatitude, southLongitude));
self.coordinateBounds = coordinateBounds;
UIImage *tempImg = self.tempImageArray[0];
if (self.groundOverlay == nil) {
MAGroundOverlay *groundOverlay = [MAGroundOverlay groundOverlayWithBounds:coordinateBounds icon:tempImg];
[self.mapView addOverlay:groundOverlay];
// self.mapView.visibleMapRect = groundOverlay.boundingMapRect;///可見區域, 設定的該范圍可能會被調整為適合地圖窗口顯示的范圍
self.groundOverlay = groundOverlay;
} else {
// 之后,切換【氣溫】和【降水】時,更新MAGroundOverlay
[self.groundOverlay setGroundOverlayWithBounds:coordinateBounds icon:tempImg];
}
// 之后,切換【氣溫】和【降水】時,更新MAGroundOverlay
// [self.groundOverlay setGroundOverlayWithBounds:coordinateBounds icon:[UIImage imageNamed:@""]];
self.itemCell = 0;
_timer = [NSTimer scheduledTimerWithTimeInterval:0.14 target:self selector:@selector(startIconRunLoop) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
[self.timer setFireDate:[NSDate distantFuture]]; // 暫停(需,手動控制開關,所以暫時不立即啟動)
// [_timer fire]; // 立即 啟動
} else {
if (_timer) {
[_timer invalidate]; // 停止,從Runlop中移除
_timer = nil; // 放在內存溢出,重置為nil
}
}
}
- (void)showRain_Action {
// 降水 圖片
if (self.raderMapDateModel != nil) {
if (_timer) {
[_timer invalidate]; // 停止,從Runlop中移除
_timer = nil; // 放在內存溢出,重置為nil
}
if (self.groundOverlay != nil) {
[self.mapView removeOverlay:self.groundOverlay]; // 點擊臺風的時候,刪掉覆蓋物層,在需要的時候重新創建
self.groundOverlay = nil;
}
if (self.commonPolyline != nil) {
[self.mapView removeOverlay:self.commonPolyline]; // 刪除 繪制折線overlay
self.commonPolyline = nil;
}
for (MAAnnotationMoveAnimation *animation in [self.typhoon_AnimatedAnnotation allMoveAnimations]) {
[animation cancel]; // 結束動畫
}
if (self.typhoon_AnimatedAnnotation != nil) {
[self.mapView removeAnnotation:self.typhoon_AnimatedAnnotation]; // 刪除 【點平滑移動】(這里特指 臺風的圖片)overlay
self.typhoon_AnimatedAnnotation = nil;
}
// if (self.typhoon_PointAnnotation != nil) {
// [self.mapView removeAnnotation:self.typhoon_PointAnnotation]; // 刪除 【點標記??】(這里特指 臺風的圖片)overlay
// self.typhoon_PointAnnotation = nil;
// }
if (self.locatinWeather_AnimatedAnnotation != nil) {
[self.mapView removeAnnotation:self.locatinWeather_AnimatedAnnotation]; // 刪除 【自定義氣泡】Annotation
self.locatinWeather_AnimatedAnnotation = nil;
}
self.currentImageArray = [self.rainImageArray copy];
[self create_TempTimeline];
self.progress_playBut.selected = NO;
self.progress_beginTimeL.text = [NSString stringWithFormat:@"%@", self.raderMapDateModel.precip.list[0].timeStr];
self.progress_endTimeL.text = [NSString stringWithFormat:@"%@", self.raderMapDateModel.precip.list[self.raderMapDateModel.precip.list.count-1].timeStr];
self.progress_tipsL.text = self.raderMapDateModel.precip.list[0].timeStr;
self.progress_tipsL.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(15+40 -30), [UIScreen mainScreen].bounds.size.height - k_Height_TabBar - [UIScreen mainScreen].bounds.size.width/375*(19+44+22), [UIScreen mainScreen].bounds.size.width/375*60, [UIScreen mainScreen].bounds.size.width/375*22);
// 東北角、西南角的經緯度
NSArray *areaArray = [self.raderMapDateModel.precip.area componentsSeparatedByString:@","];
CGFloat southLatitude = [areaArray[0] floatValue];
CGFloat southLongitude = [areaArray[1] floatValue];
CGFloat northLatitude = [areaArray[2] floatValue];
CGFloat northLongitude = [areaArray[3] floatValue];
// 【氣溫】圖片覆蓋物:
// 前:東北角,后:西南角
MACoordinateBounds coordinateBounds = MACoordinateBoundsMake(CLLocationCoordinate2DMake(northLatitude, northLongitude),CLLocationCoordinate2DMake(southLatitude, southLongitude));
self.coordinateBounds = coordinateBounds;
UIImage *tempImg = self.rainImageArray[0];
if (self.groundOverlay == nil) {
MAGroundOverlay *groundOverlay = [MAGroundOverlay groundOverlayWithBounds:coordinateBounds icon:tempImg];
[self.mapView addOverlay:groundOverlay];
// self.mapView.visibleMapRect = groundOverlay.boundingMapRect;///可見區域, 設定的該范圍可能會被調整為適合地圖窗口顯示的范圍
self.groundOverlay = groundOverlay;
} else {
// 之后,切換【氣溫】和【降水】時,更新MAGroundOverlay
[self.groundOverlay setGroundOverlayWithBounds:coordinateBounds icon:tempImg];
}
self.itemCell = 0;
_timer = [NSTimer scheduledTimerWithTimeInterval:0.14 target:self selector:@selector(startIconRunLoop) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
[self.timer setFireDate:[NSDate distantFuture]]; // 暫停(需,手動控制開關)
// [_timer fire]; // 立即 啟動
} else {
if (_timer) {
[_timer invalidate]; // 停止,從Runlop中移除
_timer = nil; // 放在內存溢出,重置為nil
}
}
}
// 多張圖片,使用NSTimer 循環
- (void)startIconRunLoop {
if (self.itemCell < self.currentImageArray.count && self.currentImageArray.count > 0) {
UIImage *currentImage = self.currentImageArray[self.itemCell];
NSLog(@"???????? self.itemCell = %ld", self.itemCell);
if (self.tempBut.selected || self.rainBut.selected) {
if (self.groundOverlay != nil) {
[self.mapView removeOverlay:self.groundOverlay]; // 點擊臺風的時候,刪掉覆蓋物層,在需要的時候重新創建
self.groundOverlay = nil;
}
// 根據bounds值和icon生成GroundOverlay
self.groundOverlay = [MAGroundOverlay groundOverlayWithBounds:self.coordinateBounds icon:currentImage];
// self.mapView.visibleMapRect = self.groundOverlay.boundingMapRect; // 可見區域, 設定的該范圍可能會被調整為適合地圖窗口顯示的范圍
[self.mapView addOverlay:self.groundOverlay];
/** 【天氣 標注氣泡】 */
if (self.locatinWeather_AnimatedAnnotation != nil) {
[self.mapView removeAnnotation:self.locatinWeather_AnimatedAnnotation]; // 刪除 【自定義氣泡】Annotation
self.locatinWeather_AnimatedAnnotation = nil;
}
if (self.locatinWeather_AnimatedAnnotation == nil && self.locationWeatherModel != nil) {
MAAnimatedAnnotation *locatinWeather_AnimatedAnnotation = [[MAAnimatedAnnotation alloc] init];
locatinWeather_AnimatedAnnotation.coordinate = CLLocationCoordinate2DMake(self.locationWeatherModel.latitude, self.locationWeatherModel.longitude); // 初始位置
// locatinWeather_AnimatedAnnotation.title = self.raderMapDateModel.typhoon[0].nameCn;
self.locatinWeather_AnimatedAnnotation = locatinWeather_AnimatedAnnotation;
[self.mapView addAnnotation:self.locatinWeather_AnimatedAnnotation];
[self.mapView selectAnnotation:self.locatinWeather_AnimatedAnnotation animated:YES]; // 選中狀態(顯示標注view)
}
// else {
// self.locatinWeather_AnimatedAnnotation.coordinate = CLLocationCoordinate2DMake(self.locationWeatherModel.latitude, self.locationWeatherModel.longitude); // 初始位置
// [self.mapView selectAnnotation:self.locatinWeather_AnimatedAnnotation animated:YES];
// }
CGFloat lineWidth = [UIScreen mainScreen].bounds.size.width/375*225/(self.currentImageArray.count -1);
self.progress_currentLineView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(40-1) + lineWidth*self.itemCell, 0, [UIScreen mainScreen].bounds.size.width/375*2, [UIScreen mainScreen].bounds.size.width/375*22);
if (([UIScreen mainScreen].bounds.size.width/375*(15 + 40 - 30) + lineWidth*self.itemCell) < [UIScreen mainScreen].bounds.size.width/375*(15+40 -30)) {
self.progress_tipsL.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(15 + 40 -30), [UIScreen mainScreen].bounds.size.height - k_Height_TabBar - [UIScreen mainScreen].bounds.size.width/375*(19+44+22), [UIScreen mainScreen].bounds.size.width/375*60, [UIScreen mainScreen].bounds.size.width/375*22);
} else if (([UIScreen mainScreen].bounds.size.width/375*(15 + 40 - 30) + lineWidth*self.itemCell) > [UIScreen mainScreen].bounds.size.width/375*(15+40 +225 -30)) {
self.progress_tipsL.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(15 + 40 +225-30), [UIScreen mainScreen].bounds.size.height - k_Height_TabBar - [UIScreen mainScreen].bounds.size.width/375*(19+44+22), [UIScreen mainScreen].bounds.size.width/375*60, [UIScreen mainScreen].bounds.size.width/375*22);
} else {
self.progress_tipsL.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(15 + 40 - 30) + lineWidth*self.itemCell, [UIScreen mainScreen].bounds.size.height - k_Height_TabBar - [UIScreen mainScreen].bounds.size.width/375*(19+44+22), [UIScreen mainScreen].bounds.size.width/375*60, [UIScreen mainScreen].bounds.size.width/375*22);
}
if (self.tempBut.selected) {
self.progress_tipsL.text = self.raderMapDateModel.temp.list[self.itemCell].timeStr;
} else if (self.rainBut.selected) {
self.progress_tipsL.text = self.raderMapDateModel.precip.list[self.itemCell].timeStr;
}
} else if (self.typhoonBut.selected) {
}
self.itemCell = self.itemCell + 1;
} else {
self.itemCell = 0;
[self startIconRunLoop];
}
}
- (void)dealloc {
[_timer invalidate]; // 停止,從Runlop中移除
_timer = nil; // 放在內存溢出,重置為nil
}
#pragma mark -- -- -- -- 點擊【氣溫】、【降水】時,或點擊【tabBar雷達】時,有數據的情況下:創建 氣溫“時間刻度” -- -- -- --
- (void)create_TempTimeline {
// 每個小格子的寬度:(因為,無論是“氣溫”還是“降水”時,第一個刻度和最后一個刻度的豎線,位置都不邊,計算單個小格子的寬度下邊這個,第一個格子前一半+最后一個格子的后一半 = 一個整格子)
CGFloat lineWidth = [UIScreen mainScreen].bounds.size.width/375*225/(self.currentImageArray.count -1);
[self.progress_lineBackView removeAllSubviews]; // 刪除所有子view(刪除時間刻度,重新創建)
self.progress_lineBackView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(40) - lineWidth/2, 0, [UIScreen mainScreen].bounds.size.width/375*225 + lineWidth, [UIScreen mainScreen].bounds.size.width/375*44);
self.progress_lineBackView.backgroundColor = UIColor.clearColor;//RGBA(212, 229, 225, 1);
for (int i=0; i < self.currentImageArray.count; i++) {
// 分成一塊塊的小格子,時間刻度在每個塊的中間(因為 每次 個數和寬度可能會發生變化,所以最好每次都重新創建)
UIView *lineBackView = [[UIView alloc] initWithFrame:CGRectMake(0 + lineWidth*i, 0, lineWidth, [UIScreen mainScreen].bounds.size.width/375*11)];
lineBackView.backgroundColor = UIColor.clearColor;//RGBA(255, 193, 188, 0.5);
[self.progress_lineBackView addSubview:lineBackView];
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake((lineWidth/2 + lineWidth*i) - [UIScreen mainScreen].bounds.size.width/375*0.5, 0, 1, [UIScreen mainScreen].bounds.size.width/375*11)];
lineView.backgroundColor = RGBA(193, 162, 122, 1);
[self.progress_lineBackView addSubview:lineView];
}
}
#pragma mark -- -- -- -- 時間刻度軸:手指滑動 Touch 觸摸 -- -- -- --
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
// 手指觸摸 開始
UITouch *touch = touches.anyObject;
CGPoint point = [touch locationInView:self.progress_view];
NSLog(@"手指觸摸 開始: 手指所在 折線圖??上的位置 point.X ==== %f,\n point.Y ==== %f", point.x, point.y);
// 每個時間刻度的距離
CGFloat lineWidth = [UIScreen mainScreen].bounds.size.width/375*225/(self.currentImageArray.count -1);
CGFloat touch_Point_X = point.x;
if (CGRectContainsPoint(self.progress_view.frame, point) && point.x >= ([UIScreen mainScreen].bounds.size.width/375*40 - lineWidth/2) && point.x <= ([UIScreen mainScreen].bounds.size.width/375*(40 + 225) + lineWidth/2)) {
NSInteger indexItem = (touch_Point_X - [UIScreen mainScreen].bounds.size.width/375*40 + lineWidth/2) / lineWidth;
self.itemCell = indexItem;
UIImage *currentImage = self.currentImageArray[self.itemCell];
NSLog(@"???????? self.itemCell = %ld", self.itemCell);
// 顯示該位置的刻度
if (self.groundOverlay != nil) {
[self.mapView removeOverlay:self.groundOverlay]; // 點擊臺風的時候,刪掉覆蓋物層,在需要的時候重新創建
self.groundOverlay = nil;
}
// 根據bounds值和icon生成GroundOverlay
self.groundOverlay = [MAGroundOverlay groundOverlayWithBounds:self.coordinateBounds icon:currentImage];
// self.mapView.visibleMapRect = self.groundOverlay.boundingMapRect;
[self.mapView addOverlay:self.groundOverlay];
CGFloat lineWidth = [UIScreen mainScreen].bounds.size.width/375*225/(self.currentImageArray.count -1);
self.progress_currentLineView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(40-1) + lineWidth*self.itemCell, 0, [UIScreen mainScreen].bounds.size.width/375*2, [UIScreen mainScreen].bounds.size.width/375*22);
if (([UIScreen mainScreen].bounds.size.width/375*(15 + 40 - 30) + lineWidth*self.itemCell) < [UIScreen mainScreen].bounds.size.width/375*(15+40 -30)) {
self.progress_tipsL.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(15 + 40 -30), [UIScreen mainScreen].bounds.size.height - k_Height_TabBar - [UIScreen mainScreen].bounds.size.width/375*(19+44+22), [UIScreen mainScreen].bounds.size.width/375*60, [UIScreen mainScreen].bounds.size.width/375*22);
} else if (([UIScreen mainScreen].bounds.size.width/375*(15 + 40 - 30) + lineWidth*self.itemCell) > [UIScreen mainScreen].bounds.size.width/375*(15+40 +225 -30)) {
self.progress_tipsL.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(15 + 40 +225-30), [UIScreen mainScreen].bounds.size.height - k_Height_TabBar - [UIScreen mainScreen].bounds.size.width/375*(19+44+22), [UIScreen mainScreen].bounds.size.width/375*60, [UIScreen mainScreen].bounds.size.width/375*22);
} else {
self.progress_tipsL.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(15 + 40 - 30) + lineWidth*self.itemCell, [UIScreen mainScreen].bounds.size.height - k_Height_TabBar - [UIScreen mainScreen].bounds.size.width/375*(19+44+22), [UIScreen mainScreen].bounds.size.width/375*60, [UIScreen mainScreen].bounds.size.width/375*22);
}
if (self.tempBut.selected) {
self.progress_tipsL.text = self.raderMapDateModel.temp.list[self.itemCell].timeStr;
} else if (self.rainBut.selected) {
self.progress_tipsL.text = self.raderMapDateModel.precip.list[self.itemCell].timeStr;
} else {
}
/** 【天氣 標注氣泡】 */
if (self.locatinWeather_AnimatedAnnotation != nil) {
[self.mapView removeAnnotation:self.locatinWeather_AnimatedAnnotation]; // 刪除 【自定義氣泡】Annotation
self.locatinWeather_AnimatedAnnotation = nil;
}
if (self.locatinWeather_AnimatedAnnotation == nil && self.locationWeatherModel != nil) {
MAAnimatedAnnotation *locatinWeather_AnimatedAnnotation = [[MAAnimatedAnnotation alloc] init];
locatinWeather_AnimatedAnnotation.coordinate = CLLocationCoordinate2DMake(self.locationWeatherModel.latitude, self.locationWeatherModel.longitude); // 初始位置
// locatinWeather_AnimatedAnnotation.title = self.raderMapDateModel.typhoon[0].nameCn;
self.locatinWeather_AnimatedAnnotation = locatinWeather_AnimatedAnnotation;
[self.mapView addAnnotation:self.locatinWeather_AnimatedAnnotation];
[self.mapView selectAnnotation:self.locatinWeather_AnimatedAnnotation animated:YES]; // 選中狀態(顯示標注view)
}
NSLog(@"?? 觸摸 對了");
} else {
NSLog(@"觸摸 錯錯錯?");
}
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
// 手指觸摸 移動
// iOS 怎么樣獲取手指在view上滑動的起點坐標與終點坐標
UITouch *touch = touches.anyObject;
CGPoint point = [touch locationInView:self.progress_view];
NSLog(@"移動中: 手指所在 折線圖??上的位置 point.X ==== %f,\n point.Y ==== %f", point.x, point.y);
// 每個時間刻度的距離
CGFloat lineWidth = [UIScreen mainScreen].bounds.size.width/375*225/(self.currentImageArray.count -1);
CGFloat touch_Point_X = point.x;
// 如果矩形不為null,或空,并且該點位于矩形內,返回YES,在范圍外面 返回NO
if (CGRectContainsPoint(self.progress_view.frame, point) && point.x >= ([UIScreen mainScreen].bounds.size.width/375*40 - lineWidth/2) && point.x <= ([UIScreen mainScreen].bounds.size.width/375*(40 + 225) + lineWidth/2)) {
NSInteger indexItem = (touch_Point_X - [UIScreen mainScreen].bounds.size.width/375*40 + lineWidth/2) / lineWidth;
self.itemCell = indexItem;
UIImage *currentImage = self.currentImageArray[self.itemCell];
NSLog(@"???????? self.itemCell = %ld", self.itemCell);
// 顯示該位置的刻度
if (self.groundOverlay != nil) {
[self.mapView removeOverlay:self.groundOverlay]; // 點擊臺風的時候,刪掉覆蓋物層,在需要的時候重新創建
self.groundOverlay = nil;
}
// 根據bounds值和icon生成GroundOverlay
self.groundOverlay = [MAGroundOverlay groundOverlayWithBounds:self.coordinateBounds icon:currentImage];
// self.mapView.visibleMapRect = self.groundOverlay.boundingMapRect;
[self.mapView addOverlay:self.groundOverlay];
CGFloat lineWidth = [UIScreen mainScreen].bounds.size.width/375*225/(self.currentImageArray.count -1);
self.progress_currentLineView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(40-1) + lineWidth*self.itemCell, 0, [UIScreen mainScreen].bounds.size.width/375*2, [UIScreen mainScreen].bounds.size.width/375*22);
if (([UIScreen mainScreen].bounds.size.width/375*(15 + 40 - 30) + lineWidth*self.itemCell) < [UIScreen mainScreen].bounds.size.width/375*(15+40 -30)) {
self.progress_tipsL.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(15 + 40 -30), [UIScreen mainScreen].bounds.size.height - k_Height_TabBar - [UIScreen mainScreen].bounds.size.width/375*(19+44+22), [UIScreen mainScreen].bounds.size.width/375*60, [UIScreen mainScreen].bounds.size.width/375*22);
} else if (([UIScreen mainScreen].bounds.size.width/375*(15 + 40 - 30) + lineWidth*self.itemCell) > [UIScreen mainScreen].bounds.size.width/375*(15+40 +225 -30)) {
self.progress_tipsL.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(15 + 40 +225-30), [UIScreen mainScreen].bounds.size.height - k_Height_TabBar - [UIScreen mainScreen].bounds.size.width/375*(19+44+22), [UIScreen mainScreen].bounds.size.width/375*60, [UIScreen mainScreen].bounds.size.width/375*22);
} else {
self.progress_tipsL.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/375*(15 + 40 - 30) + lineWidth*self.itemCell, [UIScreen mainScreen].bounds.size.height - k_Height_TabBar - [UIScreen mainScreen].bounds.size.width/375*(19+44+22), [UIScreen mainScreen].bounds.size.width/375*60, [UIScreen mainScreen].bounds.size.width/375*22);
}
if (self.tempBut.selected) {
self.progress_tipsL.text = self.raderMapDateModel.temp.list[self.itemCell].timeStr;
} else if (self.rainBut.selected) {
self.progress_tipsL.text = self.raderMapDateModel.precip.list[self.itemCell].timeStr;
} else {
}
/** 【天氣 標注氣泡】 */
if (self.locatinWeather_AnimatedAnnotation != nil) {
[self.mapView removeAnnotation:self.locatinWeather_AnimatedAnnotation]; // 刪除 【自定義氣泡】Annotation
self.locatinWeather_AnimatedAnnotation = nil;
}
if (self.locatinWeather_AnimatedAnnotation == nil && self.locationWeatherModel != nil) {
MAAnimatedAnnotation *locatinWeather_AnimatedAnnotation = [[MAAnimatedAnnotation alloc] init];
locatinWeather_AnimatedAnnotation.coordinate = CLLocationCoordinate2DMake(self.locationWeatherModel.latitude, self.locationWeatherModel.longitude); // 初始位置
// locatinWeather_AnimatedAnnotation.title = self.raderMapDateModel.typhoon[0].nameCn;
self.locatinWeather_AnimatedAnnotation = locatinWeather_AnimatedAnnotation;
[self.mapView addAnnotation:self.locatinWeather_AnimatedAnnotation];
[self.mapView selectAnnotation:self.locatinWeather_AnimatedAnnotation animated:YES]; // 選中狀態(顯示標注view)
}
NSLog(@"?? 觸摸 對了");
} else {
NSLog(@"觸摸 錯錯錯?");
}
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
// 手指觸摸 結束
NSLog(@"手指觸摸 結束");
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
// 取消觸摸時
NSLog(@"取消觸摸");
}
#pragma mark ****** 4、臺風:軌跡 “高德地圖:【繪制折線】、
#pragma mark ------------ 【設置UI】
- (void)setupUI {
_tempBut = [UIButton buttonWithType:UIButtonTypeCustom];
[_tempBut setBackgroundImage:[UIImage imageNamed:@"raderMap_氣溫_select_icon"] forState:UIControlStateSelected];
[_tempBut setBackgroundImage:[UIImage imageNamed:@"raderMap_氣溫_noSelect_icon"] forState:UIControlStateNormal];
_tempBut.adjustsImageWhenHighlighted = NO; // 下面的背景圖片可以保證圖片不變灰
[self.navigationBar addSubview:self.tempBut];
[self.tempBut makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.navigationBar);
make.left.mas_equalTo(self.navigationBar.mas_left).offset([UIScreen mainScreen].bounds.size.width/375*15);
make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*93);
make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*44);
}];
[self.tempBut addTarget:self action:@selector(clickRaderMapBotton:) forControlEvents:UIControlEventTouchUpInside];
_rainBut = [UIButton buttonWithType:UIButtonTypeCustom];
[_rainBut setBackgroundImage:[UIImage imageNamed:@"raderMap_降水_select_icon"] forState:UIControlStateSelected];
[_rainBut setBackgroundImage:[UIImage imageNamed:@"raderMap_降水_noSelect_icon"] forState:UIControlStateNormal];
_rainBut.adjustsImageWhenHighlighted = NO; // 下面的背景圖片可以保證圖片不變灰
[self.navigationBar addSubview:self.rainBut];
[self.rainBut makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.navigationBar);
make.centerX.equalTo(self.navigationBar);
make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*93);
make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*44);
}];
[self.rainBut addTarget:self action:@selector(clickRaderMapBotton:) forControlEvents:UIControlEventTouchUpInside];
_typhoonBut = [UIButton buttonWithType:UIButtonTypeCustom];
[_typhoonBut setBackgroundImage:[UIImage imageNamed:@"raderMap_臺風_select_icon"] forState:UIControlStateSelected];
[_typhoonBut setBackgroundImage:[UIImage imageNamed:@"raderMap_臺風_noSelect_icon"] forState:UIControlStateNormal];
_typhoonBut.adjustsImageWhenHighlighted = NO; // 下面的背景圖片可以保證圖片不變灰
[self.navigationBar addSubview:self.typhoonBut];
[self.typhoonBut makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.navigationBar);
make.right.mas_equalTo(self.navigationBar.mas_right).offset(-[UIScreen mainScreen].bounds.size.width/375*15);
make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*93);
make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*44);
}];
[self.typhoonBut addTarget:self action:@selector(clickRaderMapBotton:) forControlEvents:UIControlEventTouchUpInside];
self.tempBut.selected = YES;
self.rainBut.selected = NO;
self.typhoonBut.selected = NO;
}
/** 布局:(氣溫+降水)自定義功能 */
- (void)setupTempAndRainOtherFunction {
/// 氣候等級
_climateRank_BackView = [[UIView alloc] init];
self.climateRank_BackView.backgroundColor = RGBA(248, 246, 242, 1);
self.climateRank_BackView.layer.cornerRadius = [UIScreen mainScreen].bounds.size.width/375*26/2;
self.climateRank_BackView.layer.shadowColor = RGBA(0, 0, 0, 0.12).CGColor;
self.climateRank_BackView.layer.shadowOffset = CGSizeMake(0, 0);
self.climateRank_BackView.layer.shadowRadius = [UIScreen mainScreen].bounds.size.width/375*8;
self.climateRank_BackView.layer.shadowOpacity = 1;
[self.view addSubview:self.climateRank_BackView];
[self.climateRank_BackView makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view.mas_top).offset(k_Height_StatusBar+k_Height_NavContentBar + [UIScreen mainScreen].bounds.size.width/375*14);
make.left.mas_equalTo(self.view.mas_left).offset([UIScreen mainScreen].bounds.size.width/375*20);
make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*173);
make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*26);
}];
_climateRank_Img = [[UIImageView alloc] init];
self.climateRank_Img.image = [UIImage imageNamed:@"raderMap_TempRankColor_icon"];
[self.climateRank_BackView addSubview:self.climateRank_Img];
[self.climateRank_Img makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.climateRank_BackView);
make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*115);
make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*6);
}];
_climateRank_minTipsL = [[UILabel alloc] init];
_climateRank_minTipsL.text = @"-40°";
_climateRank_minTipsL.textColor = RGBA(0, 0, 0, 0.5);
_climateRank_minTipsL.textAlignment = NSTextAlignmentLeft;
_climateRank_minTipsL.font = FontSourceHanSerifCN_Medium([UIScreen mainScreen].bounds.size.width/375*10);
[self.climateRank_BackView addSubview:self.climateRank_minTipsL];
[self.climateRank_minTipsL makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.climateRank_BackView);
make.left.mas_equalTo(self.climateRank_BackView.mas_left).offset([UIScreen mainScreen].bounds.size.width/375*8);
}];
_climateRank_maxTipsL = [[UILabel alloc] init];
_climateRank_maxTipsL.text = @"40°";
_climateRank_maxTipsL.textColor = RGBA(0, 0, 0, 0.5);
_climateRank_maxTipsL.textAlignment = NSTextAlignmentRight;
_climateRank_maxTipsL.font = FontSourceHanSerifCN_Medium([UIScreen mainScreen].bounds.size.width/375*10);
[self.climateRank_BackView addSubview:self.climateRank_maxTipsL];
[self.climateRank_maxTipsL makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.climateRank_BackView);
make.right.mas_equalTo(self.climateRank_BackView.mas_right).offset(-[UIScreen mainScreen].bounds.size.width/375*8);
}];
_currentPositionBut = [UIButton buttonWithType:UIButtonTypeCustom];
[_currentPositionBut setBackgroundImage:[UIImage imageNamed:@"raderMap_currentLocation_icon"] forState:UIControlStateNormal];
_currentPositionBut.adjustsImageWhenHighlighted = NO; // 點擊時圖片 不變灰色
[self.currentPositionBut addTarget:self action:@selector(click_backCurrentPositionAction) forControlEvents:UIControlEventTouchUpInside];
self.currentPositionBut.layer.masksToBounds = YES;
self.currentPositionBut.layer.cornerRadius = [UIScreen mainScreen].bounds.size.width/375*44/2;
[self.view addSubview:self.currentPositionBut];
[self.currentPositionBut makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.view.mas_bottom).offset(-(k_Height_TabBar + [UIScreen mainScreen].bounds.size.width/375*19));
make.right.mas_equalTo(self.view.mas_right).offset(-[UIScreen mainScreen].bounds.size.width/375*12);
make.width.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*44);
}];
/** 播放/暫停:氣候變化,(可以 手動滑動 刻度) */
_progress_BackView = [[UIView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/375*15, [UIScreen mainScreen].bounds.size.height - k_Height_TabBar - [UIScreen mainScreen].bounds.size.width/375*(19+44), [UIScreen mainScreen].bounds.size.width/375*290, [UIScreen mainScreen].bounds.size.width/375*44)];
_progress_BackView.backgroundColor = RGBA(248, 246, 242, 1);
self.progress_BackView.layer.cornerRadius = [UIScreen mainScreen].bounds.size.width/375*44/2;
self.progress_BackView.layer.shadowColor = RGBA(0, 0, 0, 0.12).CGColor;
self.progress_BackView.layer.shadowOffset = CGSizeMake(0, 0);
self.progress_BackView.layer.shadowRadius = [UIScreen mainScreen].bounds.size.width/375*4;
self.progress_BackView.layer.shadowOpacity = 1;
[self.view addSubview:self.progress_BackView];
/** 時間刻度的父view ,寬度:(290 - 40 -25) = 225*/
_progress_lineBackView = [[UIView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/375*40, 0, [UIScreen mainScreen].bounds.size.width/375*(225), [UIScreen mainScreen].bounds.size.width/375*44)];
self.progress_lineBackView.backgroundColor = UIColor.clearColor;//RGBA(212, 229, 225, 1);
[self.progress_BackView addSubview:self.progress_lineBackView];
_progress_beginTimeL = [[UILabel alloc] init];
_progress_beginTimeL.text = @" ";
_progress_beginTimeL.textColor = RGBA(153, 153, 153, 1);
_progress_beginTimeL.textAlignment = NSTextAlignmentLeft;
_progress_beginTimeL.font = FontSourceHanSerifCN_Medium([UIScreen mainScreen].bounds.size.width/375*12);
[self.progress_BackView addSubview:self.progress_beginTimeL];
[self.progress_beginTimeL makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self.progress_BackView.mas_centerY).offset([UIScreen mainScreen].bounds.size.width/375*11); make.left.mas_equalTo(self.progress_BackView.mas_left).offset([UIScreen mainScreen].bounds.size.width/375*40);
}];
_progress_endTimeL = [[UILabel alloc] init];
_progress_endTimeL.text = @" ";
_progress_endTimeL.textColor = RGBA(153, 153, 153, 1);
_progress_endTimeL.textAlignment = NSTextAlignmentRight;
_progress_endTimeL.font = FontSourceHanSerifCN_Medium([UIScreen mainScreen].bounds.size.width/375*12);
[self.progress_BackView addSubview:self.progress_endTimeL];
[self.progress_endTimeL makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self.progress_BackView.mas_centerY).offset([UIScreen mainScreen].bounds.size.width/375*11);
make.right.mas_equalTo(self.progress_BackView.mas_right).offset(-[UIScreen mainScreen].bounds.size.width/375*25);
}];
_progress_currentLineView = [[UIView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/375*(40-1), 0, [UIScreen mainScreen].bounds.size.width/375*2, [UIScreen mainScreen].bounds.size.width/375*22)];
self.progress_currentLineView.backgroundColor = RGBA(193, 162, 122, 1);
[self.progress_BackView addSubview:self.progress_currentLineView];
// Touch觸摸 view視圖
_progress_view = [[UIView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/375*0, 0, [UIScreen mainScreen].bounds.size.width/375*290, [UIScreen mainScreen].bounds.size.width/375*44)];
self.progress_view.userInteractionEnabled = YES;
self.progress_view.backgroundColor = RGBA(88, 255, 88, 0);
[self.progress_BackView addSubview:self.progress_view];
// 開關:播放、暫停
_progress_playBut = [UIButton buttonWithType:UIButtonTypeCustom];
[_progress_playBut setBackgroundImage:[UIImage imageNamed:@"raderMap_playChange_icon"] forState:UIControlStateNormal];
[_progress_playBut setBackgroundImage:[UIImage imageNamed:@"raderMap_pauseChange_icon"] forState:UIControlStateSelected];
_progress_playBut.adjustsImageWhenHighlighted = NO; // 點擊時圖片 不變灰色
[self.progress_BackView addSubview:self.progress_playBut];
[self.progress_playBut makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.progress_BackView);
make.left.mas_equalTo(self.progress_BackView.mas_left).offset([UIScreen mainScreen].bounds.size.width/375*10);
make.width.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*24);
}];
[self.progress_playBut addTarget:self action:@selector(click_playClimateChangeAction) forControlEvents:UIControlEventTouchUpInside];
_progress_tipsL = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/375*(15+40 - 30), [UIScreen mainScreen].bounds.size.height - k_Height_TabBar - [UIScreen mainScreen].bounds.size.width/375*(19+44+22), [UIScreen mainScreen].bounds.size.width/375*60, [UIScreen mainScreen].bounds.size.width/375*22)];
_progress_tipsL.backgroundColor = RGBA(193, 162, 122, 1);
_progress_tipsL.textColor = UIColor.whiteColor;
_progress_tipsL.textAlignment = NSTextAlignmentCenter;
_progress_tipsL.font = FontSourceHanSerifCN_Medium([UIScreen mainScreen].bounds.size.width/375*9);
_progress_tipsL.layer.masksToBounds = YES;
_progress_tipsL.layer.cornerRadius = [UIScreen mainScreen].bounds.size.width/375*22/2;
[self.view addSubview:self.progress_tipsL];
self.climateRank_minTipsL.text = @"-40°";
self.climateRank_maxTipsL.text = @"40°";
self.progress_BackView.hidden = YES;
self.progress_tipsL.hidden = YES;
}
@end