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