5

我是 iOS 编程新手,对 COCOA Touch 知之甚少。我正在尝试在谷歌地图底部的屏幕上添加一个按钮,以便为用户提供返回上一个屏幕的选项。我只知道这UIButton是一个子类,UIView我们可以通过使其成为该类的子视图来使按钮出现在视图上。以前 iOS 过去默认使用谷歌地图,MKMapView我在 Internet 上的书籍中看到示例显示应用程序的屏幕截图,其中按钮或文本框会出现在地图上。但是现在只是在界面构建器中拖动按钮并没有帮助。

MKMapView 上的文本框

这是我的代码:

视图控制器.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <GoogleMaps/GoogleMaps.h>


@interface ViewController : UIViewController 

@property (weak, nonatomic) IBOutlet UIButton *btn;


@end

视图控制器.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

请让我知道如何做到这一点。

谢谢。

4

2 回答 2

6

在当前视图上构建普通 UI,然后添加(DON'T do )的GMSMapViewas subview(at index0 )self.viewself.view = mapView;

这是重要的代码:

mapView = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
[self.view insertSubview:mapView atIndex:0];

在 0 处插入地图视图index会将其余对象设置在前面。

于 2013-05-21T05:01:26.737 回答
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];
于 2013-08-20T02:58:54.003 回答