在iPhone的Google地图中添加像UISlider或UIButtom这样的子视图

5
我是一名新手iOS编程者,对COCOA Touch的了解非常有限。我想在Google地图上方添加一个按钮,以便向用户提供返回前一页的选项。我只知道UIButtonUIView的子类,我们可以通过将其作为该类的子视图来使按钮出现在视图上。之前iOS默认使用Google Maps,在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

请告诉我如何完成这个任务。谢谢。

Google Maps和MKMapView没有关联,具有不同的功能集/遵循不同的方法。 - Daij-Djan
但是...只需将滑块视图作为地图的子视图即可。这样就可以与MKMapView很好地配合使用。 - Daij-Djan
1
我知道在MKMapView上它可以正常工作。但据我所知,MKMapView不会提供Google地图。它只会提供由苹果提供的地图。这是我的真正问题。 - MK Singh
一个视图就是一个视图。当你将UISlider拖到Google地图视图上时,会出现什么问题? - Craig
2
@Craig 我以以下方式声明了一个Google地图的mapView: mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; mapView_.myLocationEnabled = YES; self.view = mapView_; 并将按钮*btn作为mapview_的子视图。 [mapView_ addSubview:btn]; [mapView bringSubviewToFront:_btn];但是它仍然无法在屏幕上显示。 - MK Singh
显示剩余2条评论
2个回答

6

在当前视图上进行正常的 UI 构建,然后将 GMSMapView 作为 self.viewsubview(位于index 0),不要使用 self.view = mapView;

下面是关键代码:

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

在索引为0处插入地图视图将使其余对象在前面。

我在我的程序中添加了你建议的代码。但是当我运行程序并点击应用程序中的按钮以进入地图视图时,应用程序挂起并没有加载下一个屏幕,地图应该出现的位置。 所以我又回到了旧代码 - self.view = mapView_ - MK Singh
你能把你的代码发过来看看问题出在哪里吗?哦,确保将上面的代码放在viewDidLoad方法中,而不是loadView。 - Raul Claus
3
这种方法行不通。GMSMapView根本没有出现。 - AdamG
在 Swift 中,供您参考:self.view.insertSubview(view, at: 0) - Jorge Cordero

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];

我提出这个问题的目的基本上是为了回到上一个屏幕。所以我通过将地图嵌入到导航控制器中来解决了我的问题。但唯一的缺点是,即使我只对一个小按钮感兴趣,导航栏也会占用屏幕的一些空间。 - MK Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接