无法将谷歌地图 GMSMapView 放在主视图的子视图中? [英] Cannot put a google maps GMSMapView in a subview of main main view?

查看:17
本文介绍了无法将谷歌地图 GMSMapView 放在主视图的子视图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决这个问题!我想将一个谷歌地图 GMSMapView 添加到一个 UIView 中,它只是我的 ViewController 的主要 UIView 的一部分>.

它应该很简单...我用故事板创建了一个我想要的大小的 UIView 并将它放在主 UIView 中.

来自接口文件的片段:

@interface MapViewController : UIViewController @property(nonatomic) IBOutlet GMSMapView *mapView;

在主 UIView 中使用与此 UIView 链接的 mapView.

我在实施中做了什么:

@synthesize mapView = _mapView;- (void)viewDidLoad{[超级viewDidLoad];//加载视图后进行任何其他设置.GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683经度:151.2086缩放:10];_mapView = [GMSMapView mapWithFrame:_mapView.bounds camera:camera];_mapView.myLocationEnabled = YES;}

这应该可行,不是吗?我得到的是我的内部 UIView 是空的.

如果我按照 google 的入门指南 Google 地图集成文档实例化地图,它可以工作,但它将地图分配给 MAIN UIView,这是另一回事.

我什至尝试将故事板中的 mapView 类从 UIView 更改为 GMSMapView.地图显示在内部视图中,但它以一种奇怪的方式进行了初始化

  1. 系统抛出错误说

<块引用>

无法制作完整的帧缓冲区"

并且大大减慢了视图的加载速度(在模拟器上需要 2-3 秒)

  1. 地图在 viewDidLoad 方法中完成的任何相机更改中都没有响应.

有什么建议吗?

我在 StackOverflow 上阅读了一些帖子,但找不到有效的解决方案 :(

解决方案

根据其他答案,以下是三种实际可行的方法.

  1. 在 XIB 上放置一个视图并将其类更改为 XIB 构建器中的 GMSMapView.请参阅下面的 *map1.或者...
  2. 全部编码.添加地图作为主视图的子视图.请参阅下面的 *map2.或者...
  3. 将地图添加为另一个子视图的子视图,该子视图已经在带有插座的 XIB 中.*地图3如下.

.h

@interface GPViewController : UIViewController@property (strong, nonatomic) IBOutlet GMSMapView *map1;@property (strong, nonatomic) IBOutlet UIView *plainViewOnXIBHoldsMap3;@结尾

.m

- (void)viewDidLoad{[超级viewDidLoad];GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.0经度:151.20缩放:6];/* 选项 1. XIB 上的视图是类 GSMMapView */self.map1.camera = 相机;/* 选项 2. 添加地图作为子视图 */GMSMapView *map2 = [GMSMapView mapWithFrame:CGRectMake(0, 0, 100, 100) camera:camera];[self.view addSubview:map2];/* 选项 3. 将地图添加到已经在 XIB 上的子视图 */GMSMapView *map3 = [GMSMapView mapWithFrame:self.plainViewOnXIBHoldsMap3.bounds camera:camera];[self.plainViewOnXIBHoldsMap addSubview:map3];}

I'm struggling with this problem! I want to add a google maps GMSMapView into a UIView that is only a portion of the main UIView of my ViewController.

It should be simple... I created with the storyboard a UIView of the size I want and put it in the main UIView.

Snippet from Interface file:

@interface MapViewController : UIViewController <CLLocationManagerDelegate>

@property(nonatomic) IBOutlet GMSMapView *mapView;

With the mapView linked with this UIView inside the main UIView.

What I do in the implementation:

@synthesize mapView = _mapView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
                                                            longitude:151.2086
                                                                 zoom:10];
    _mapView = [GMSMapView mapWithFrame:_mapView.bounds camera:camera];
    _mapView.myLocationEnabled = YES;

}

This should work, shouldn't it? What I get is that my internal UIView is empty.

If I instantiate a map following google's starting guide Google Map Integration Document, it works but it assigns the map to the MAIN UIView and thats a different thing.

I tried even changing the class of my mapView in the storyboard from UIView to GMSMapView. The map is showed in the inner view but it is initialized in a strange way making

  1. The system throwing an error saying

"Failed to make complete frame buffer"

and slowing down a lot the loading of the view (it takes 2-3 seconds on the simulator)

  1. The map not responding in any camera change done in the viewDidLoad method.

Any suggestions?

I read some posts here on StackOverflow but couldn't find a valid solution :(

解决方案

Based on other answers, here are three ways that actually work.

  1. Put a view on the XIB and change its class to GMSMapView in XIB builder. See *map1 below. Or...
  2. Code it all. Add the map as a subview of the main view. See *map2 below. Or...
  3. Add the map as a subview of another subview already in the XIB with an outlet. *map3 below.

.h

@interface GPViewController : UIViewController
@property (strong, nonatomic) IBOutlet GMSMapView *map1;
@property (strong, nonatomic) IBOutlet UIView *plainViewOnXIBHoldsMap3;
@end

.m

- (void)viewDidLoad{
    [super viewDidLoad];
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.0
                                                        longitude:151.20
                                                             zoom:6];

    /* Option 1. view on XIB is class GSMMapView */
    self.map1.camera = camera;

    /* Option 2. add a map as a subview */
    GMSMapView *map2 = [GMSMapView mapWithFrame:CGRectMake(0, 0, 100, 100) camera:camera];
    [self.view addSubview:map2];

    /* Option 3. add a map to a subview already on the XIB */
    GMSMapView *map3 = [GMSMapView mapWithFrame:self.plainViewOnXIBHoldsMap3.bounds camera:camera];
    [self.plainViewOnXIBHoldsMap addSubview:map3];
}

这篇关于无法将谷歌地图 GMSMapView 放在主视图的子视图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆