Using the Google Maps SDK in views other than the

2019-01-24 15:10发布

问题:

I am trying to use the Google Maps SDK for iOS in a subview of the main view which I created in the storyboard and linked to the view controller via an IBOutlet (I called it extraView, subclassed from UIView). When I follow the steps in the SDK getting started guide, the SDK works just fine, but it uses the uppermost view in the hierarchy (the main view), which I don't want. I want my map to be in a smaller portion of the screen and use the rest of the screen for something else. When I attempt to assign the mapView_ object (see the getting started guide) to self.extraView instead of self.view, the whole screen is black and I get an error in the console output:

"Application windows are expected to have a root view controller at the end of application launch"

Has anyone else figured this out? I can't find anything in the documentation and the sample code Google provides does not use a storyboard.

回答1:

Here's how...

  • add a UIView into the view controller where you're working
  • set it's class to be GMSMapView in the identity inspector.

Then control-drag it to your code as you would for any other outlet.

You can lazily instantiate it in its setter...

- (void) setMapView:(GMSMapView *)mapView {
    if (!mapView) {
        mapView = [[GMSMapView alloc] initWithFrame:mapView.bounds];
    }
    _mapView = mapView;
}

To display a map Google's sample code becomes...

  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:1.285
                                                          longitude:103.848
                                                               zoom:12];
  self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];


回答2:

I solved my problem just removing the loadview code that i took from the example. Just adding a view as sberley said should works.

just on thing more, on the identity inspector, that attribute that you have to change is class, at least it is on xcode 4.5