我正在制作一个使用GMSMapView的iPhone应用程序,我希望能够在地图上添加工具栏。这是我的代码:
#import "MapViewController.h"
#import <GoogleMaps/GoogleMaps.h>
@interface MapViewController ()
@end
@implementation MapViewController
{
GMSMapView *mapView_;
id<GMSMarker> myMarker;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)back:(id)sender
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
// You don't need to modify the default initWithNibName:bundle: method.
- (void)loadView
{
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:30.616083 longitude:-96.338908 zoom:13];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
mapView_.delegate = self;
self.view = mapView_;
GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
options.position = CLLocationCoordinate2DMake(30.616083, -96.338908);
options.title = @"College Station";
options.snippet = @"Texas";
[mapView_ addMarkerWithOptions:options];
}
#pragma mark - GMSMapViewDelegate
- (void)mapView:(GMSMapView *)mapView
didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
[myMarker remove];
NSLog(@"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
GMSMarkerOptions *marker = [[GMSMarkerOptions alloc] init];
marker.position = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);
marker.title = @"Tap Event";
// (icon for marker) marker.icon = [UIImage imageNamed:@"house"];
myMarker = [mapView_ addMarkerWithOptions:marker];
}
@end
在我的故事板中,我有一个工具栏,但我不知道如何让它显示在GMSMapView上。我是iOS的新手,已经在网上搜索过解决方案但是没有运气GMSMapView。
答案 0 :(得分:0)
因为您将视图设置为mapView_,所以在StoryBoard中创建的视图将被覆盖。我使用以下代码添加了带有分段控件和按钮的工具栏:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
UINavigationBar *navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, screenBounds.size.width, 44)];
UINavigationItem *navItems = [[UINavigationItem alloc]init];
UIBarButtonItem *barButtonRight = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(SettingsButtonPressed)];
UISegmentedControll *segControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Opt1",@"Opt2",@"Opt3", nil]];
UIImage *imageSettings= [UIImage imageNamed:@"36-toolbox.png"];
[segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segControl setSelectedSegmentIndex:0];
[segControl setFrame:CGRectMake(10, 5, 150, 32)];
[segControl addTarget:self action:@selector(KiesVC:) forControlEvents:UIControlEventValueChanged];
[barButtonRight setImage:imageSettings];
[navItems setRightBarButtonItem:barButtonRight];
[navItems setTitleView:segControl];
[navBar setItems:[NSArray arrayWithObject:navItems] animated:NO];
然后通过调用:
添加工具栏[self.view addSubview:navBar];
希望有所帮助