首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌地图开发工具包导致iOS 7泄漏

谷歌地图开发工具包导致iOS 7泄漏
EN

Stack Overflow用户
提问于 2014-01-20 19:32:37
回答 1查看 1.5K关注 0票数 4

当谷歌地图加载时,分析器中出现泄漏。我已经根据google的示例代码创建了一个非常简单的View Controller,我发现在地图加载时我得到了一个漏洞。我相信泄漏是在SDK本身。有没有人遇到过这个问题并找到了解决方案?

基本视图控制器

代码语言:javascript
复制
//
//  JRCViewController.m
//  GoogleMapsInterface
//
//  Created by Jake Cunningham on 15/01/2014.
//  Copyright (c) 2014 Jake Cunningham. All rights reserved.
//

#import "JRCViewController.h"

@interface JRCViewController (){
BOOL firstLocationUpdate_;
GMSMapView *mapView;
}


@end


@implementation JRCViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
                                                            longitude:151.2086
                                                                 zoom:6];
    mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

    [mapView addObserver:self
               forKeyPath:@"myLocation"
                  options:NSKeyValueObservingOptionNew
                  context:NULL];

    self.view = mapView;

    dispatch_async(dispatch_get_main_queue(), ^{
        mapView.myLocationEnabled = YES;
    });

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {

    if (!firstLocationUpdate_) {
        // If the first location update has not yet been recieved, then jump to that
        // location.
        firstLocationUpdate_ = YES;
        CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
        mapView.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
                                                        zoom:14];
    }
}



@end
EN

回答 1

Stack Overflow用户

发布于 2014-07-23 22:37:19

我找到了问题的原因。像你一样,我认为他们的“演示代码”应该按原样工作。不理解这一行

代码语言:javascript
复制
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
                                                            longitude:151.2086
                                                                 zoom:6];

演示中的第一行实际上是问题所在。如果你不在澳大利亚(这就是这个定位点所在的地方),你会将整个世界地图(磁贴)加载到应用程序的内存中,这是错误的!

如果您大致知道您的地图将使用哪个大陆/国家/州,或者更好!在显示地图之前获取用户的位置,并在该位置加载地图。

因此,地图的初始化应该是这样的:

CLLocation *location = self getUserLocation;//可能来自共享首选项,即使它距离用户实际所在的位置100英里,也比加载另一个大陆要好。

然后你的viewDidLoad会是这样的

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];
     CLLocation *location = [self getUserLocation]; //<== very important!
    // Do any additional setup after loading the view, typically from a nib.
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:location.coordinate.latitude
                                                            longitude:location.coordinate.longitude
                                                                 zoom:15];
    mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

    [mapView addObserver:self
               forKeyPath:@"myLocation"
                  options:NSKeyValueObservingOptionNew
                  context:NULL];

    self.view = mapView;

    dispatch_async(dispatch_get_main_queue(), ^{
        mapView.myLocationEnabled = YES;
    });

}

缩放级别对此->也有影响缩放越大,加载到内存中的平铺越少。

此外,我还在viewWillDisappear中添加了代码(假设当再次需要给定的ViewController时,viewDidLoad将再次运行)

代码语言:javascript
复制
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
        [self.mapView clear];
        [self.mapView removeFromSuperview] ;
        self.mapView.delegate = nil;
        self.mapView = nil ;
}

这帮助我的应用程序从必须使用140MB的ram减少到只有56 MB!当app normal介于40和45之间时。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21232913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档