如果不查看更多代码,我无法立即说出导致此问题的原因,但是对于像 GMSMapView 这样的“昂贵”对象,我可能会创建一个类别来创建共享 GMSMapView 并以编程方式添加它(如果您还没有这样做的话)。您的类别可能如下所示:
// GMSMapView+MyAdditions.h
#import <GoogleMaps/GoogleMaps.h>
@interface GMSMapView (MyAdditions)
+ (GMSMapView *)sharedMapView;
@end
// GMSMapView+MyAdditions.m
#import "GMSMapView+MyAdditions.h"
@implementation GMSMapView (MyAdditions)
+ (GMSMapView *)sharedMapView {
static GMSMapView *mapView;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
CLLocation *location = [[CLLocation alloc] initWithLatitude:40.7127 longitude:-74.0059];
GMSCameraPosition *cameraPosition = [GMSCameraPosition cameraWithLatitude:location.coordinate.latitude
longitude:location.coordinate.longitude
zoom:16.0];
mapView = [GMSMapView mapWithFrame:[UIScreen mainScreen].bounds camera:cameraPosition];
});
return mapView;
}
@end