如何在iOS中的Google地图上添加button?

我是iOS编程的新手,我已经下载了iOS的谷歌地图sdk,并按照他们网站上的说明(如链接https://developers.google.com/maps/documentation/ios/start )所示,在我的应用程序中获取地图。

现在我正试图在谷歌地图底部的屏幕上添加一个button,让用户可以select返回到前一个屏幕。

我只知道UIButton是UIView的一个子类,我们可以通过使它成为该类的子视图来使button出现在视图上。 以前iOS默认使用Google Map来使用Google Maps,而且我在互联网上的书籍和书籍上看到了一些例子,在屏幕上显示了应用程序的屏幕快照,其中button或文本框会出现在地图上。 但是现在只需拖动界面生成器中的button并不能帮助Google地图的SDK。

这是我的代码:

ViewController.h

#import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import <GoogleMaps/GoogleMaps.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UIButton *btn; @end 

ViewController.m

 #import "ViewController.h" #import <MapKit/MapKit.h> #import <GoogleMaps/GoogleMaps.h> #import <CoreLocation/CoreLocation.h> @interface ViewController () @end @implementation ViewController { GMSMapView *mapView_; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)loadView { CLLocationManager *locationManager = [[CLLocationManager alloc] init]; locationManager.distanceFilter = kCLDistanceFilterNone; locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; [locationManager startUpdatingLocation]; //Latitude and longitude of the current location of the device. double lati = locationManager.location.coordinate.latitude; double longi = locationManager.location.coordinate.longitude; NSLog(@"Latitude = %f", lati); NSLog(@"Longitude = %f", longi); CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:lati longitude:longi]; // Create a GMSCameraPosition that tells the map to display the coordinate GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:lati longitude:longi zoom:11.5]; mapView_ = [GMSMapView mapWithFrame:[[UIScreen mainScreen] bounds] camera:camera]; mapView_.myLocationEnabled = YES; self.view = mapView_; // Creates a marker in the center of the map. GMSMarker *marker = [[GMSMarker alloc] init]; marker.position = CLLocationCoordinate2DMake(lati, longi); marker.title = @"It's Me"; marker.snippet = @"My Location"; marker.map = mapView_; [mapView_ addSubview:_btn]; [mapView_ bringSubviewToFront:_btn]; } @end 

你可以看到,在最后2行我已经把button的子视图的mapview,并试图把它前面。 但是这并没有帮助。 请让我知道我缺less的是什么,或者如果有另一种方式来使用其他function来做到这一点。

还请检查我创build的故事板的截图,以便您可以更好地理解我在这里要做的事情。

故事板的屏幕截图

谢谢。

GMSMapViewUIView子类,因此您可以添加子视图作为任何其他视图

试试这个代码

 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(mapView_.bounds.size.width - 110, mapView_.bounds.size.height - 30, 100, 20); button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin; [button setTitle:@"Button" forState:UIControlStateNormal]; [mapView_ addSubview:button]; 

它添加了100x20button作为GMSMapView的子视图,位于右下angular。 我已经testing过,button可以像其他任何视图一样被触摸

编辑:同时将所有代码从-loadView移动到-viewDidLoad 。 使用IB创buildUI时,从不调用-loadView方法。 Docs to -loadView说:

如果使用Interface Builder创build视图并初始化视图控制器,则不得重写此方法。

编辑2:我相信当你使用Interface Builder创build视图层次时,你不能像你在做的那样重置self.view属性。

在你的-viewDidLoad做这个

 [self.view addSubview: mapView_]; 

代替

 self.view = mapView_; 

如果您将GMSMapView传递给self.view属性,那么从这一点来看,地图只是控制器中的视图。 那我相信,你不能看到你的IB创build的button的原因。

通常使用InterfaceBuilder进行视图,并将mapView放置在0索引子视图中:

 mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera]; mapView_.myLocationEnabled = YES; [self.view insertSubview:mapView_ atIndex:0]; 

感谢劳尔克劳斯从这个线程

你可以自定义mapView的大小,改变:

 mapView_ = [GMSMapView mapWithFrame:_mapHolder.bounds camera:camera]; mapView_.myLocationEnabled = YES; [_mapHolder insertSubview:mapView_ atIndex:0]; 

其中mapHolder是IB内部制作的UIView。

我面临同样的问题,修复非常简单,只需重写这个UIViewController方法:
-(void) viewWillAppear:(BOOL)animated并把你的UIButton创build逻辑内的方法。

例:

 -(void) viewWillAppear:(BOOL)animated { locationButton = [UIButton buttonWithType:UIButtonTypeCustom]; locationButton.frame = CGRectMake(0, 30, self.view.frame.size.width/6, self.view.frame.size.height/6); [locationButton setImage:[UIImage imageNamed:@"location_enabled.png"] forState:UIControlStateNormal]; [self.view addSubview:locationButton]; }