1

我用工厂设计模式实现谷歌地图。但是当我加载地图视图时地图不显示。当我在不使用工厂模式的情况下实现相同时,我可以成功加载它。请帮我解决这个问题。下面显示的是代码。

     //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
4

1 回答 1

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];

 }
于 2013-09-29T17:43:52.147 回答