如何让app多个页面只用一个地图实例

来源:互联网 发布:阿里云免费域名注册 编辑:程序博客网 时间:2024/05/16 15:52

地图唯一实例,放置不同地图页面切换,地图页面和非地图页面来回切换导致内存持续上涨问题。
声明
//对外开放全局变量,获取高德地图唯一实例指针
//由于高德地图只能初始化一次,重复释放后初始化会出现内存不能正常释放的情况,所以使用全局高德地图指针
//由于ARC机制是若有有一个地方应用,就不会把对象释放了掉,所以全局百度地图指针能保证高德地图的唯一实例不会被释放掉。
//使用高德地图的地方全部通过该函数引用获取唯一实例
//若获取到后先判断是否为空(初始化过),若为空直接初化它,若非空把该对象强制转化为MAMapView *类型的指针,调整frame,设置代理就可以使用了
//由于非高德地图的代理对象不允许定义MAMapView类的对象指针,所以用它的基类(UIView *)指向它。
//高德地图基类指针专用
@property(nonatomic,strong) UIView *mymapView;

pragma mark MAMap

-(UIView *)getMAMapViewInstance;
-(void)setMAMapViewInstance : (UIView *)mymapView;

实现
-(UIView *)getMAMapViewInstance
{
FLDDLogDebug(@”函数”);
return self.mymapView;
}

-(void)setMAMapViewInstance : (UIView *)mymapView
{
FLDDLogDebug(@”函数”);
if(nil == self.mymapView)
{
self.mymapView = mymapView;
}
}

3D地图中的使用
MAMapView namapView = (MAMapView )([[Singleton sharedInstance] getMAMapViewInstance]);
if(nil == namapView)
{
_mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, kControlWidth, kControlHeight)];
[[Singleton sharedInstance] setMAMapViewInstance:_mapView];
}
else
{
CGRect newRect = CGRectMake(0, 0, kControlWidth, kControlHeight);
namapView.frame = newRect;
_mapView = namapView;
}

_mapView.delegate = self;[self.view addSubview:_mapView];

// //定位
_mapView.showsUserLocation = YES;
//地图跟着位置和方向移动
[_mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES];

_mapView.showsCompass = NO;//楼块隐藏_mapView.buildingsDisabled=YES;//后台定位 可持久记录位置信息。_mapView.pausesLocationUpdatesAutomatically = NO;//不能旋转_mapView.rotateEnabled=NO;_mapView.distanceFilter = DISTANCE_FILTER;

// _mapView.desiredAccuracy = kCLLocationAccuracyHundredMeters;
// mapView.showsScale = YES;
// [mapView setZoomLevel:KmaxLEVEL animated:NO];
//自定义精度圈样式 去除精度圈
_mapView.delegate = self;

热力图中的使用
MAMapView namapView = (MAMapView )([[Singleton sharedInstance] getMAMapViewInstance]);
if(nil == namapView)
{
namapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, kControlWidth, kControlHeight)];
[[Singleton sharedInstance] setMAMapViewInstance:namapView];
self.mapView = namapView;
}
else
{
namapView.frame = self.view.bounds;
self.mapView = namapView;
}

_mapView.delegate = self;_mapView.showsUserLocation = YES;_mapView.showsUserLocation = YES;

导航中的使用
FLDDLogDebug(@”函数”);
self.mapView = (MAMapView *)[[Singleton sharedInstance] getMAMapViewInstance];
if(nil == _mapView)
{
_mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, kControlWidth, kControlHeight)];
[[Singleton sharedInstance] setMAMapViewInstance:_mapView];
}
else
{
self.mapView.frame = self.view.bounds;
}
_mapView.delegate = self;
_mapView.showsUserLocation = YES;
[self.view addSubview:self.mapView];

高德地图终于支持多实例了,不用这种曲线救国的方式实现多实例了。
废除:// self.naviViewController = [[AMapNaviViewController alloc] initWithMapView:mymapView delegate:self];
采用新的代理函数:
self.naviViewController = [[AMapNaviViewController alloc] initWithDelegate:self];

0 0
原创粉丝点击