我是iOS编程的新手,对COCOA Touch知之甚少。我试图在Google地图底部的屏幕上添加一个按钮,以便用户选择返回上一个屏幕。我只知道UIButton
是UIView
的子类,我们可以通过使按钮成为该类的子视图来使按钮出现在视图上。以前iOS默认情况下在MKMapView
中使用Google地图,我在互联网上看过书中的示例,显示应用程序的屏幕截图,其中按钮或文本框将出现在地图上。但现在只需拖动界面构建器中的按钮就无济于事了。
这是我的代码:
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
请让我知道如何做到。
感谢。
答案 0 :(得分:6)
在当前视图上构建正常的用户界面,然后将GMSMapView
添加为subview
的{{1}}(index
0)(不要做{{1} }})
以下是重要代码:
self.view
在self.view = mapView;
0处插入地图视图会将其余对象设置在前面。
答案 1 :(得分:1)
我建议以编程方式创建按钮,而不是使用storyBoard,我在“Google Maps SDK版本:1.4.0.4450”上进行了测试,结果确实如此。
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10, 10, 100, 40);
[button setTitle:@"title" forState:UIControlStateNormal];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.279526
longitude:-96.987305
zoom:2];
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];;
self.view = self.mapView;
[self.view addSubview:button];