【问题标题】:Adding a subview like UISlider or UIButtom in Google Maps in iPhone在 iPhone 的 Google 地图中添加子视图,如 UISlider 或 UIButton
【发布时间】:2013-05-08 08:50:01
【问题描述】:

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

这是我的代码:

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 不相关,并且具有不同的功能集/遵循不同的方法
  • 但是 .. 只是让滑块成为地图的子视图。 MKMapView 可以正常工作
  • 我知道它可以与 MKMapView 一起工作。但据我所知,MKMapView 不会给我谷歌地图。它会给我苹果提供的地图。那是我真正的问题。
  • 一个视图是一个视图是一个视图。将 UISlider 拖到 Google 地图视图上时出了什么问题?
  • @Craig 我以下列方式为谷歌地图声明了一个 mapView mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; mapView_.myLocationEnabled = YES; self.view = mapView_; 并制作了一个按钮 *btn 作为 mapview_ 的子视图。 [mapView_addSubview:_btn]; [mapView_bringSubviewToFront:_btn]; 但是在屏幕上还是不可见

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


【解决方案1】:

在当前视图上构建普通 UI,然后将 GMSMapView 添加为 subviewindex 0)的 self.view(不要这样做 self.view = mapView;

这是重要的代码:

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

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

【讨论】:

  • 我在我的程序中添加了你建议的代码。但是当我运行程序并单击应用程序中的按钮以转到地图视图时,应用程序挂起并且没有加载应该出现地图的下一个屏幕。所以我再次回到旧代码 - self.view = mapView_
  • 你能把你的代码发过来看看问题出在哪里吗?哦,请确保将上面的代码放在 viewDidLoad 方法中,而不是 loadView。
  • 这种方法行不通。 GMSMapView 根本没有出现。
  • 仅供参考:self.view.insertSubview(view, at: 0)
【解决方案2】:

我建议不使用 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-11-11
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 1970-01-01
  • 2016-01-07
  • 1970-01-01
  • 1970-01-01
  • 2018-02-05
相关资源
最近更新 更多