0

I'm trying to display my Google Map inside a custom UIView instead of in self.view. Currently, my code works fine if I set it to display in self.view; the map is displayed with all applied styles and specified camera. But the minute I try and display it in a custom UIView (self.mapView), the map displays but it's completely empty (ie. no camera set, no markers, nothing). Any idea why this is?

MapViewController.h

@interface MapViewController : ViewController <CLLocationManagerDelegate, GMSMapViewDelegate> 

@property (strong, nonatomic) IBOutlet GMSMapView *mapView;
@property (nonatomic, strong) IBOutlet GMSCameraPosition *camera;

MapViewController.m

-(void)viewDidLoad {

        self.camera = [GMSCameraPosition cameraWithLatitude:-33.868
                                                                 longitude:151.2086
                                                                      zoom:12];


          self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:self.camera];
          self.mapView.settings.compassButton = YES;
          self.mapView.settings.myLocationButton = YES;


         [self.mapView addObserver:self
                    forKeyPath:@"myLocation"
                       options:NSKeyValueObservingOptionNew
                       context:NULL];

         [self.view addSubview:_mapView];


     NSBundle *mainBundle = [NSBundle mainBundle];
        NSURL *styleUrl = [mainBundle URLForResource:@"style" withExtension:@"json"];
        NSError *error;


        // Set the map style by passing the URL for style.json.
        GMSMapStyle *style = [GMSMapStyle styleWithContentsOfFileURL:styleUrl error:&error];
        NSLog(@"This is what %@", style);

        if (!style) {
          NSLog(@"The style definition could not be loaded: %@", error);
        } else {

            NSLog(@"Style Loaded");
        }



        self.mapView.mapStyle = style;

      dispatch_async(dispatch_get_main_queue(), ^{
             self.mapView.myLocationEnabled = YES;
         });



    }

    - (void)dealloc {
        [self.mapView removeObserver:self
                    forKeyPath:@"myLocation"
                       context:NULL];
    }

    - (void)observeValueForKeyPath:(NSString *)keyPath
                          ofObject:(id)object
                            change:(NSDictionary *)change
                           context:(void *)context {
      if (!firstLocationUpdate) {

        firstLocationUpdate = YES;
        CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey];
          self.mapView.camera = [GMSCameraPosition cameraWithTarget:location.coordinate
                                                         zoom:14];
      }
    }
Brittany
  • 1,359
  • 4
  • 24
  • 63
  • 1) with this `mapWithFrame:CGRectZero` you make a view (the map's view) with height and width = 0; why? 2) You already have the mapview - don't try to add again the same view `[self.view addSubview:_mapView];` https://stackoverflow.com/a/26778538/1702413 – TonyMkenu Jun 02 '20 at 10:29
  • Ahhh totally missed that - and problem solved. You rock @TonyMkenu! Thank you. – Brittany Jun 08 '20 at 00:53

0 Answers0