将Google地图分配给子视图时出现黑屏

时间:2013-04-10 18:41:29

标签: ios google-maps google-maps-sdk-ios

我正在尝试在Google地图视图中加载适用于iOS6的地方。 如何设置Map的框架? 目前它是全屏的

 -(void)loadView {

   GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:10.00989
                                                        longitude:76.316142
                                                             zoom:15];
   mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
   self.view = mapView_;

   }

我尝试在当前视图中创建一个新的(小)视图并在其中添加地图,但此时页面没有加载。它显示一个完整的黑屏

 -(void)loadView {

  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:10.00989
                                                        longitude:76.316142
                                                             zoom:15];
   mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];

   [self.view Addsubview:newView];
   self.newView = mapView_;

   }

4 个答案:

答案 0 :(得分:3)

终于找到了解决方案。 删除loadview方法,并将以下代码放在view did load方法中:

-(void)viewDidLoad{
    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                            longitude:151.20
                                                                 zoom:6];

    mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, 100, 100) camera:camera];
    [self.view addSubview:mapView_];
    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.

    mapView_.myLocationEnabled = YES;

    // Creates a marker in the center of the map.
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
    marker.title = @"Sydney";
    marker.snippet = @"Australia";
    marker.map = mapView_;
}

答案 1 :(得分:1)

尝试

mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera];

[self.view addSubview:mapView_];

答案 2 :(得分:1)

如果你输入loadView它没有视图.. xib甚至没有加载... 把它放在 viewDidLoad

并做

mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
[self.view addSubview:mapView_];

答案 3 :(得分:0)

通过从我的代码中删除func loadView修复了我的问题

    override func loadView() {

}

或者将这行代码放在loadView

    override func loadView() {
            self.view = GMSMapView(frame: UIScreen.main.bounds)
}