ios 无法将谷歌地图 GMSMapView 放在主视图的子视图中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15417811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot put a google maps GMSMapView in a subview of main main view?
提问by AndreaZ
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
.
我正在努力解决这个问题!我想将一个谷歌地图添加GMSMapView
到一个UIView
只是UIView
我的ViewController
.
It should be simple... I created with the storyboard a UIView
of the size I want and put it in the main UIView
.
它应该很简单......我用UIView
我想要的大小的故事板 a 创建并将它放在 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
.
将 mapView 与UIView
main 中的this 链接在一起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.
这应该有效,不是吗?我得到的是我的内部 UIView 是空的。
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.
如果我按照 google 的入门指南Google Map Integration Document实例化地图,它可以工作,但它将地图分配给 MAIN UIView,这是另一回事。
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
我什至尝试将故事板中 mapView 的类从UIView
更改为GMSMapView
。地图显示在内部视图中,但它以一种奇怪的方式初始化
- 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)
并大大减慢了视图的加载速度(在模拟器上需要 2-3 秒)
- The map not responding in any camera change done in the
viewDidLoad
method.
- 地图在
viewDidLoad
方法中完成的任何相机更改中都没有响应。
Any suggestions?
有什么建议?
I read some posts here on StackOverflow but couldn't find a valid solution :(
我在 StackOverflow 上阅读了一些帖子,但找不到有效的解决方案:(
回答by cloudsurfin
Based on other answers, here are three ways that actually work.
根据其他答案,以下是三种实际有效的方法。
- Put a view on the XIB and change its class to GMSMapView in XIB builder. See *map1 below. Or...
- Code it all. Add the map as a subview of the main view. See *map2 below. Or...
- Add the map as a subview of another subview already in the XIB with an outlet. *map3 below.
- 在 XIB 上放置一个视图并将其类更改为 XIB 构建器中的 GMSMapView。请参阅下面的 *map1。或者...
- 全部编码。添加地图作为主视图的子视图。请参阅下面的 *map2。或者...
- 将地图添加为另一个子视图的子视图,该子视图已经在带有出口的 XIB 中。*下面的地图3。
.h
。H
@interface GPViewController : UIViewController
@property (strong, nonatomic) IBOutlet GMSMapView *map1;
@property (strong, nonatomic) IBOutlet UIView *plainViewOnXIBHoldsMap3;
@end
.m
.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];
}
回答by Mike K
Have checked out and get working in Swift 2.0 all of the three options provided by cloudsurfin. Translating his answer for those who face the same problem in Swift 2.0:
已检查并开始在 Swift 2.0 中使用cloudsurfin提供的所有三个选项。为那些在 Swift 2.0 中遇到同样问题的人翻译他的答案:
Option1- Put a view on the XIB and change its class to GMSMapView in XIB builder.
选项 1- 在 XIB 上放置一个视图并将其类更改为 XIB 构建器中的 GMSMapView。
Option2- Code it all. Add the map as a subview of the main view.
选项 2- 全部编码。添加地图作为主视图的子视图。
Option3- Add the map as a GMSMapView subview of already existing UIView subview.
选项 3- 将地图添加为现有 UIView 子视图的 GMSMapView 子视图。
Explanation of preparations in XIB for Option1:
对Option1在 XIB 中的准备工作的说明:
1) Set a class for your view:
1)为您的视图设置一个类:
2) Don't forget to connect your view with the outlet in code:
2)不要忘记在代码中将您的视图与插座连接起来:
import UIKit
import GoogleMaps
class MapViewController: UIViewController {
@IBOutlet weak var map1 : GMSMapView!
@IBOutlet weak var map3 : UIView!
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.cameraWithLatitude(53.9,
longitude: 27.5667, zoom: 6)
// Option 1. view on XIB is class GSMMapView
self.map1.camera = camera;
// Option 2. add a map as a subview
let map2 = GMSMapView.mapWithFrame(CGRectMake(0, 64, 320, 460), camera:camera)
self.view.addSubview(map2)
// Option 3. add a map to a subview of UIView class that is already on the XIB
let map3 = GMSMapView.mapWithFrame(self.plainViewOnXIBHoldsMap3.bounds, camera:camera)
self.plainViewOnXIBHoldsMap3.addSubview(map3)
}
}
Hope this note helps someone :)
希望这篇笔记对某人有所帮助:)
回答by Eric Forbes
My suggestions:
我的建议:
- Link the UIView from your storyboard to your header file as a UIView instance variable, not a GMSMapView
- Change the mapWithFrame:camera: method to be set to your linked UIView's bounds
- At the end of the viewDidLoad: method, set the UIView instance variable to your GMSMapView, or add a subview. I've had trouble with this as well, and fooling around with this usually will work.
- 将故事板中的 UIView 作为 UIView 实例变量而不是 GMSMapView 链接到头文件
- 更改 mapWithFrame:camera: 方法以设置为您链接的 UIView 的边界
- 在 viewDidLoad: 方法的末尾,将 UIView 实例变量设置为您的 GMSMapView,或者添加一个子视图。我也遇到了这个问题,并且在这方面鬼混通常会奏效。
.h
。H
@interface MapViewController: UIViewController <CLLocationManagerDelegate>
{
IBOutlet UIView *mapViewOnSreen;
}
.m
.m
- (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:mapViewOnScreen.bounds camera:camera];
_mapView.myLocationEnabled = YES;
mapViewOnScreen = _mapView
//OR [self.view addSubview:_mapView];
//OR [mapViewOnScreen addSubview:_mapView]; <--This worked for me
}
*Edit: I created a small UIView inside the main view using IB. I followed the steps listed and I was able to view the map inside the small UIView when I set [mapViewOnScreen addSubview:_mapView];
*编辑:我使用 IB 在主视图中创建了一个小的 UIView。我按照列出的步骤操作,当我设置 [mapViewOnScreen addSubview:_mapView]; 时,我能够查看小 UIView 中的地图。
回答by Robert Jenkins
you can do it through IB
你可以通过IB做到
.h
@property (weak, nonatomic) IBOutlet GMSMapView *theMapView;
.m
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:12];
//This next line is not needed if you have a GMSMapView outlet
//self.theMapView = [GMSMapView mapWithFrame:self.theMapView.frame camera:camera];
self.theMapView.camera=camera;
self.theMapView.mapType = kGMSTypeSatellite;
self.theMapView.delegate=self;
回答by Lirik
I was getting the black screen just because i forgot to add this line:
我得到黑屏只是因为我忘记添加这一行:
[super loadView];
回答by user2231017
I spent about a half hour trying to get this to work and then I figured out that I had it in the wrong method. The google getting started example tells you to put it in - (void)loadView but if you do that you get a black screen or a full screen map. You need to put it in - (void)viewDidLoad. Then it works.
我花了大约半个小时试图让它发挥作用,然后我发现我使用了错误的方法。谷歌入门示例告诉你把它放在 - (void)loadView 中,但如果你这样做,你会得到一个黑屏或全屏地图。您需要将其放入 - (void)viewDidLoad。然后它起作用了。
回答by Heshan Sandeepa
For Swift 3 and if you sub class the GMSMapView
对于 Swift 3,如果您将其子类化 GMSMapView
- Add
UIView
intoUIViewController
in story board
- 添加
UIView
到UIViewController
故事板
- Change class in to
GMSMapView
insteadUiView
- 将班级
GMSMapView
改为UiView
Create reference in
UIViewController
class@IBOutlet weak var mapView: GMSMapView!
As you are subclassing you do not need use that
GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mentioned in the docs. Just load map as followslet camera = GMSCameraPosition.camera(withLatitude: 51.50, longitude: -0.076, zoom: 10.0) mapView.camera = camera
In order to add marker
let marker = GMSMarker() marker.position = CLLocationCoordinate2D(latitude: 51.50, longitude: -0.076) marker.title = "London" marker.snippet = "UK" marker.map = mapView
Together as a function
override func viewDidLoad() { super.viewDidLoad() load() } func load() { //map let camera = GMSCameraPosition.camera(withLatitude: 51.50, longitude: -0.076, zoom: 10.0) mapView.camera = camera //marker let marker = GMSMarker() marker.position = CLLocationCoordinate2D(latitude: 51.50, longitude: -0.076) marker.title = "London" marker.snippet = "UK" marker.map = mapView }
在
UIViewController
课堂上创建参考@IBOutlet weak var mapView: GMSMapView!
当您进行子类化时,您不需要使用
GMSMapView.map(withFrame: CGRect.zero, camera: camera)
文档中提到的内容。只需按如下方式加载地图let camera = GMSCameraPosition.camera(withLatitude: 51.50, longitude: -0.076, zoom: 10.0) mapView.camera = camera
为了添加标记
let marker = GMSMarker() marker.position = CLLocationCoordinate2D(latitude: 51.50, longitude: -0.076) marker.title = "London" marker.snippet = "UK" marker.map = mapView
一起作为一个函数
override func viewDidLoad() { super.viewDidLoad() load() } func load() { //map let camera = GMSCameraPosition.camera(withLatitude: 51.50, longitude: -0.076, zoom: 10.0) mapView.camera = camera //marker let marker = GMSMarker() marker.position = CLLocationCoordinate2D(latitude: 51.50, longitude: -0.076) marker.title = "London" marker.snippet = "UK" marker.map = mapView }
回答by Omarj
set the UIVIEW class to be GMSMapView in the identity inspector.
在身份检查器中将 UIVIEW 类设置为 GMSMapView。
then make an IBOutlet from it :D and hola it is work :P
然后从中制作一个 IBOutlet :D 和你好它是工作 :P
回答by Urvish Modi
(void)viewDidLoad {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:14];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = @"Sydney";
marker.snippet = @"Australia";
marker.map = self.mapContainerView;
//set the camera for the map
self.mapContainerView.camera = camera;
self.mapContainerView.myLocationEnabled = YES;
}
回答by Musa almatri
Following the google tutorial for Swiftin Sep-2017
遵循2017 年 9 月的Swift谷歌教程
I fixed by putting the code in viewDidLoad like this
我通过将代码放在 viewDidLoad 中来解决这个问题
var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Create a GMSCameraPosition to display the initial center of the map
let camera = GMSCameraPosition.camera(withLatitude: 23.885942, longitude: 45.079162, zoom: 5.1)
// Set the frame size , don't forget to replace "<CustomFrame>" with the required frame size
mapView = GMSMapView.map(withFrame: "<CustomFrame>", camera: camera)
// Add the map to any view here
view.addSubview(mapView) //or customView.addSubview(mapView)
}