如何在iOS上的Google地图上添加按钮? [英] How to add a button on the Google maps in iOS?

查看:237
本文介绍了如何在iOS上的Google地图上添加按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS编程的新手,我已经下载了google地图sdk的iOS,并按照他们的网站上的说明(如链接
https://developers.google.com/maps/documentation/ios/start

,并且能够在我的应用程序中获取地图。



现在我想在谷歌地图的底部在屏幕上添加一个按钮,让用户回到选项上一个屏幕。



我只知道UIButton是UIView的子类,我们可以使一个按钮出现在视图上,使它成为该类的子视图。以前,iOS曾默认使用MKMapView使用Google地图,我在互联网上的书籍中看到了一些示例,其中显示了应用程序的屏幕截图,其中按钮或文本框会显示在地图上。但现在只需拖动界面生成器中的按钮,就不能使用google地图的SDK。



这是我的代码:



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

  #importViewController.h
#import< MapKit / MapKit.h>
#import< GoogleMaps / GoogleMaps.h>
#import< CoreLocation / CoreLocation.h>


@interface ViewController()

@end

@implementation ViewController
{
GMSMapView * mapView_ ;
}

- (void)viewDidLoad
{
[super viewDidLoad];
//在加载视图之后执行任何其他设置,通常来自nib。
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//处理可以重新创建的任何资源。
}

- (void)loadView
{
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;

locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];

//设备当前位置的经度和纬度。
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];

//创建一个GMSCameraPosition,告诉地图显示坐标

GMSCameraPosition * camera = [GMSCameraPosition cameraWithLatitude:lati
longitude:longi
zoom :11.5];

mapView_ = [GMSMapView mapWithFrame:[[UIScreen mainScreen] bounds] camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;

//在地图中心创建一个标记。
GMSMarker * marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(lati,longi);
marker.title = @It's Me;
marker.snippet = @我的位置;
marker.map = mapView_;

[mapView_ addSubview:_btn];
[mapView_ bringSubviewToFront:_btn];

}
@end

最后2行我做了按钮的mapview的子视图,并试图带它前面。但这没有帮助。请让我知道是什么,我失踪,或者如果有另一种方法来做到这一点使用一些其他功能。



还请检查故事板的屏幕截图





谢谢。

解决方案

GMSMapView UIView 的子类,因此您可以向任何其他视图添加子视图



尝试此代码

  UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
button.frame = CGRectMake(mapView_.bounds.size.width - 110,mapView_.bounds.size.height - 30,100,20);
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
[button setTitle:@ButtonforState:UIControlStateNormal];
[mapView_ addSubview:button];

它添加 100x20 按钮作为子视图 GMSMapView ,位于右下角。我已经测试它,并且按钮可以像任何其他视图中一样触摸



编辑:
也移动所有代码从 -viewDidLoad -loadView 方法在使用IB创建UI时从未被调用。文档 -loadView 说:


如果使用Interface Builder创建视图并且初始化视图控制器,你不能重写这个方法。


编辑2:
我相信当你创建视图层次使用Interface Builder,你不能重置 self.view 属性像你在做。



在您的 -viewDidLoad

中执行此操作

  [self.view addSubview:mapView_]; 

而不是

  self.view = mapView_;如果您将 GMSMapView 传递给    code> self.view 属性,地图只是从这一点上看是在控制器中。我相信,这是为什么你看不到你的IB创建的按钮。


I am new to iOS Programming and I have downloaded the google maps sdk for iOS and followed the instruction on their website ( as shown in this link https://developers.google.com/maps/documentation/ios/start ) and was able to get the map in my application.

Now I am trying to add a button on the screen at the bottom over Google maps for giving an option to the user to go back to the previous screen.

I just know that UIButton is a subclass of UIView and we can make a button appear on a view by making it the sub view of that class. Previously iOS used to use Google Maps by default by MKMapView and I have seen examples in books an on the Internet showing screen shots of apps where a button or a text box would appear on the map. But now just dragging the button in the interface builder doesn't help with the SDK of google maps.

Here is my code:

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

You can see that in the last 2 lines I have made the button the subview of mapview and tried to bring it front. But this didn't help. Please let me know what is it that I am missing or if there is another way to do this by using some other function.

Please also do check the screenshot of the storyboard which I have created so that you can understand better what I am trying to do here.

Thanks.

解决方案

GMSMapView is subclass of UIView so you can add subviews as to any other view

Try this code

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(mapView_.bounds.size.width - 110, mapView_.bounds.size.height - 30, 100, 20);
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
[button setTitle:@"Button" forState:UIControlStateNormal];
[mapView_ addSubview:button];

It adds 100x20 button as the subview of the GMSMapView, positioned to the bottom right corner. I have tested it and the button can be touched as in any other view

Edit: Also move all your code from -loadView to -viewDidLoad. -loadView method is never called when using IB to create UI. Docs to -loadView says:

If you use Interface Builder to create your views and initialize the view controller, you must not override this method.

Edit 2: I believe when you create view hierarchy using Interface Builder, you CAN NOT reset self.view property like you are doing.

Do this in your -viewDidLoad

[self.view addSubview: mapView_];

instead of

self.view = mapView_;

if you are passing GMSMapView to the self.view property, the map is only view which is in the controller from this point. Thats, I believe, the reason why u can't see your IB-created button.

这篇关于如何在iOS上的Google地图上添加按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆