ファクトリ デザイン パターンで Google マップを実装します。しかし、マップビューをロードするとマップが表示されません。ファクトリ パターンを使用せずに同じものを実装すると、正常に読み込まれました。この問題を解決するのを手伝ってください。以下にコードを示します。
//Caller
#import "ViewController.h"
#import "Constants.h"
#import "MapBuilderFactory.h"
#import "MapBuilderDelegate.h"
- (void)viewDidLoad
{
[super viewDidLoad];
id<MapBuilderDelegate> mapBuilder=[MapBuilderFactory mapWithName:GoogleMaps];
[mapBuilder initMapWithApiKey:kGoogleMapsApiKey];
UIView *mapView= [mapBuilder mapView];
[self.view addSubview:mapView];
}
MapBuilderFactory の実装
#import "MapBuilderFactory.h"
#import "GoogleMapsViewController.h"
@implementation MapBuilderFactory
+(id)mapWithName:(mapType)mapType
{
id returnValue;
switch (mapType) {
case AppleMaps:
returnValue=nil;
break;
case GoogleMaps:
returnValue=[GoogleMapsViewController new];
break;
default:
break;
}
return returnValue;
}
@end
GoogleMapsViewController の実装
@interface GoogleMapsViewController ()
@property(nonatomic,retain)GMSMapView *mapView;
@end
@implementation GoogleMapsViewController
@synthesize mapView=mapView_;
-(void)initMapWithApiKey:(NSString*)apiKey
{
[GMSServices provideAPIKey:apiKey];
}
-(UIView*)mapView
{
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = @"Sydney";
marker.snippet = @"Australia";
marker.map = mapView_;
return mapView_;
}
MapBuilderDelegate
@protocol MapBuilderDelegate <NSObject>
-(void)initMapWithApiKey:(NSString*)apiKey;
-(UIView*)mapView;
@end