Search code examples
iosobjective-cgoogle-mapsfactory-pattern

iOS Factory design patteren


I implementing Google maps with factory design pattern. But map is not displayed when i load the map view. When i implemented the same without using factory pattern, i could get it loaded successfully. please help me to fix this issue. Below shown is the code.

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

     }

Implementation of 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

Implementation of 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

Solution

  • is the problem with the view or is the map configuration? I dont see where the frame is specified. below i've added a setframe in your viewDidLoad

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