我使用工厂设计模式实施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
答案 0 :(得分:0)
是视图的问题还是地图配置?我没看到框架的指定位置。下面我在viewDidLoad中添加了一个setframe
- (void)viewDidLoad
{
[super viewDidLoad];
id<MapBuilderDelegate> mapBuilder=[MapBuilderFactory mapWithName:GoogleMaps];
[mapBuilder initMapWithApiKey:kGoogleMapsApiKey];
UIView *mapView= [mapBuilder mapView];
[mapView setFrame:CGRectMake(0.0, 0.0, 320.0, 500.0)];
[self.view addSubview:mapView];
}