2013-09-29 113 views
1

我使用工廠設計模式實施Google地圖。但是當我加載地圖視圖時地圖不顯示。當我在不使用工廠模式的情況下實現相同的功能時,我可以成功加載它。請幫我解決這個問題。下面顯示的是代碼。 GoogleMapsViewControlleriOS工廠設計模式

的MapBuilderFactory

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

    } 

實施

 #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 

實施

@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

你有沒有通過調試器?它是否遵循預期的路徑?你有沒有得到預期的物體? – rmaddy

+0

是的,我調試了,我得到了預期的對象。 – Karthick

+0

您是否嘗試過馬克建議正確設置框架? – rmaddy

回答

0

是與視圖的問題,或在地圖configur通貨膨脹?我沒有看到框架在哪裏指定。下面我在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]; 

}