Google Maps iOS [closed]

2019-09-20 15:57发布

问题:

I am trying to insert Google Maps in a CGRect in iOS. But the code below still displays the map in the entire iPhone page. How do I fix this

CGRect rect = CGRectMake(50, 10, 200, 200);
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
                                                        longitude:151.2086
                                                             zoom:6];
mapView_ = [GMSMapView mapWithFrame:rect camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
options.position = CLLocationCoordinate2DMake(-33.8683, 151.2086);
options.title = @"Sydney";
options.snippet = @"Australia";
[mapView_ addMarkerWithOptions:options];

回答1:

By default the root view of a view controller is resized to fill the screen.

You would need to add a separate view to be the root view, and then add the map view to it. For example:

self.view = [[UIView alloc] initWithFrame: CGRectZero];
[self.view addSubview: mapView_];


回答2:

A nice way that I've found to work with Google Maps Views is to:

1 - drag a new view object onto your controller or view

2 - size and position accordingly

4 - in identity inspector set class to GMSMapView

5 - control drag using the assistant to give it to your view controller as a property

6 - have everything be the exact size you specified in the editor and treat it basically like an already initialized map view!