xcode 如何将 Google Map 放入自定义 UIView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17078485/
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
How to put Google Map in a custom UIView
提问by Cedric Berger
I know this question has already been asked before, but none of the answers were really clear to me and I can't find good tutorial on Internet... So, I want to put Google maps in a different UIView from the principal one in order to be able to show my menu bar on top.
我知道之前已经问过这个问题,但没有一个答案对我来说真的很清楚,我在互联网上找不到好的教程......所以,我想将谷歌地图放在与主要的不同的 UIView 中为了能够在顶部显示我的菜单栏。
Here is my actual code :
这是我的实际代码:
#import "MapViewController.h"
#import <GoogleMaps/GoogleMaps.h>
@implementation MapViewController
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:46.809885
longitude:-71.184556
zoom:18];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view = mapView;
mapView.mapType = kGMSTypeHybrid;
}
@end
Actually, the map is all over the app. It already set the class of my UIView to GMSKMapView and made an outlet name mapView to link my UIView with the controller.
实际上,地图遍布整个应用程序。它已经将我的 UIView 的类设置为 GMSKMapView 并创建了一个出口名称 mapView 以将我的 UIView 与控制器链接起来。
I use Xcode 4.6.2
我使用 Xcode 4.6.2
Thank you for your help!
感谢您的帮助!
回答by TonyMkenu
I'm using this code:
我正在使用此代码:
//header file
//头文件
@property (strong, nonatomic) IBOutlet UIView *viewForMap;
@property (nonatomic, strong) IBOutlet GMSMapView *mapView;
@property (nonatomic, strong) IBOutlet GMSCameraPosition *camera;
//implementation file
//实现文件
self.camera = [GMSCameraPosition cameraWithLatitude:46.2220
longitude:25.2330 zoom:5
bearing:0
viewingAngle:0
];
self.mapView = [GMSMapView mapWithFrame:_viewForMap.bounds camera:_camera];
self.mapView.delegate = self;
[self.viewForMap addSubview:_mapView];
UPD
UPD
to change map type:
更改地图类型:
self.mapView.mapType = kGMSTypeHybrid; //kGMSTypeNormal kGMSTypeHybrid kGMSTypeSatellite kGMSTypeTerrain
to change again camera view:
再次更改相机视图:
_mapView.camera = [GMSCameraPosition cameraWithLatitude:newLat
longitude:newLong
zoom:1
bearing:0
viewingAngle:0
];
don't forget to add in header file:
不要忘记在头文件中添加:
<GMSMapViewDelegate>
回答by Harshit
First create outlet Of UIView
首先创建outlet的UIView
#import <GoogleMapsM4B/GoogleMaps.h>
@interface ViewController : UIViewController<GMSMapViewDelegate>
@property (strong, nonatomic) IBOutlet GMSMapView *mapView;
Add This in .h File
将此添加到 .h 文件中
Now add this in .m file in view didload method
现在在视图 didload 方法的 .m 文件中添加它
self.mapView.myLocationEnabled = YES;
self.mapView.mapType = kGMSTypeNormal;
self.mapView.settings.compassButton = YES;
self.mapView.settings.myLocationButton = YES;
self.mapView.delegate = self;