简体   繁体   中英

iOS Objective-C Google map shows only on debug when added to a subview

I'm using google maps SDK, first all was working fine but it was shown as a full screen. After adding the map into a subview, the map is no more shown. I added a breakpoint on the IBAction and noticed that after passing:

self.mapView1 = [GMSMapView mapWithFrame:_subview.bounds camera:camera];

the map is shown perfectly in the subview.

Thats my code in IBAction:

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:33.67 longitude:35.55 zoom:12];

self.mapView1 = [GMSMapView mapWithFrame:_subview.bounds camera:camera];

self.mapView1.myLocationEnabled = YES;
[_mapView1 setMinZoom:12 maxZoom:20];
self.mapView1.mapType = kGMSTypeSatellite;
self.mapView1.settings.compassButton = YES;
_mapView1.delegate = self;
[_mapView1 animateToViewingAngle:45];

// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(33.67, 35.55);
marker.title = @"City";
marker.snippet = @"Country";
marker.map = _mapView1;
[_subview addSubview:_mapView1];

thats how i want it to look like

Thanks in advance.

Try this it's working for me.

GMSMapView *customView = [[GMSMapView alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:33.67 longitude:35.55 zoom:12];

customView = [GMSMapView mapWithFrame:customView.bounds camera:camera];

customView.myLocationEnabled = YES;
[customView setMinZoom:12 maxZoom:20];
customView.mapType = kGMSTypeSatellite;
customView.delegate = self;
[customView animateToViewingAngle:45];

// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(33.67, 35.55);
marker.title = @"City";
marker.snippet = @"Country";
marker.map = customView;
[self.view addSubview:customView];

OR

GMSMapView *customView = [[GMSMapView alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:33.67 longitude:35.55 zoom:12];
_mapView = [GMSMapView mapWithFrame:customView.bounds camera:camera];

_mapView.myLocationEnabled = YES;
[_mapView setMinZoom:12 maxZoom:20];
_mapView.mapType = kGMSTypeSatellite;
_mapView.settings.compassButton = YES;
_mapView.delegate = self;
[_mapView animateToViewingAngle:45];

// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(33.67, 35.55);
marker.title = @"City";
marker.snippet = @"Country";
marker.map = _mapView;
[customView addSubview:_mapView];
[self.view addSubview:customView];

Also check your _subview hidden or not.

Since i can't comment, i will use this way to ask you to do some stuff:

1- Add a breankpoint to this line and check _subview.bounds values.

self.mapView1 = [GMSMapView mapWithFrame:_subview.bounds camera:camera];

2- While running in the simulator click on Debug > View Debugging > Capture View Hierarchy and locate your AVPlayer, check the properties on the right panel.

Check the _subview.bounds when you are creating the GMSMapView.

Depending on the time this is done, it might not be setup and you can end-up with a CGRectZero frame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM