iphone - 在 iPhone 的谷歌地图中添加像 UISlider 或 UIButtom 这样的 subview

标签 iphone ios cocoa-touch mkmapview google-maps-sdk-ios

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

Text box over MKMapView

这是我的代码:

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

请告诉我怎么做。

谢谢。

最佳答案

在当前 View 上构建普通 UI,然后将 GMSMapView 添加为 self 的 subview(在 index 0) .view(不要做 self.view = mapView;)

这里是重要的代码:

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

index 0 处插入 map View 会将其余对象设置在前面。

关于iphone - 在 iPhone 的谷歌地图中添加像 UISlider 或 UIButtom 这样的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16436352/

相关文章:

ios - API 1.1 版的 Twitter 授权

ios - 在 Swift 项目 (Empatica) 中使用 Objective-C 框架文件时出现奇怪的构建错误

ios - 如何仅设置 UIView 的左上角和右上角的cornerRadius?

iphone - AFNetworking 多次上传减慢主线程

iphone - 如何在 Core Data 中分配相关对象?

iphone - 当在应用商店状态提交应用时是无效二进制?

iphone - 如何更改模态 UIViewController 的动画样式?

iphone - UIViewController 类中的 presentModalViewController

iphone - Cocoa 数组到字符串被截断

iphone - iPhone 的 Thrift 框架