Man*_*nas 13 iphone cocoa-touch interface-builder ios google-maps-sdk-ios
我是iOS编程新手,我已经下载了适用于iOS的谷歌地图sdk,并按照其网站上的说明进行操作(如此链接https://developers.google.com/maps/documentation/ios/start所示 )并且能够在我的应用程序中获取地图.
现在,我尝试在谷歌地图底部的屏幕上添加一个按钮,为用户提供返回上一屏幕的选项.
我只知道UIButton是UIView的子类,我们可以通过使它成为该类的子视图来使一个按钮出现在视图上.以前iOS默认使用谷歌地图MKMapView,我已经在互联网上的书籍中看到了一些示例,显示了按钮或文本框出现在地图上的应用程序的屏幕截图.但现在只需拖动界面构建器中的按钮对谷歌地图的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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
您可以看到,在最后两行中,我将按钮设置为mapview的子视图,并尝试将其置于前面.但这没有帮助.请让我知道我错过了什么,或者是否有其他方法可以通过使用其他功能来实现.
另请查看我创建的故事板的屏幕截图,以便您更好地了解我在这里要做的事情.

谢谢.
Luk*_*cka 15
GMSMapView是子类,UIView因此您可以添加任何其他视图的子视图
试试这个代码
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];
Run Code Online (Sandbox Code Playgroud)
它添加了100x20按钮作为子视图GMSMapView,位于右下角.我测试了它,按钮可以像任何其他视图一样触摸
编辑:同时将所有代码-loadView移至-viewDidLoad.-loadView使用IB创建UI时永远不会调用方法.文件-loadView说:
如果使用Interface Builder创建视图并初始化视图控制器,则不得覆盖此方法.
编辑2:我相信当您使用Interface Builder创建视图层次结构时,self.view您无法重置您正在执行的属性.
在你的身上这样做 -viewDidLoad
[self.view addSubview: mapView_];
Run Code Online (Sandbox Code Playgroud)
代替
self.view = mapView_;
Run Code Online (Sandbox Code Playgroud)
如果您要传递GMSMapView给该self.view属性,则该地图仅是此时控制器中的视图.我相信,那就是你无法看到你创建的IB按钮的原因.
| 归档时间: |
|
| 查看次数: |
15756 次 |
| 最近记录: |