在iPhone中的Google地图中添加UISlider或UIButtom等子视图

MK *_*ngh 5 iphone cocoa-touch mkmapview ios google-maps-sdk-ios

我是iOS编程的新手,对COCOA Touch知之甚少.我试图在Google地图底部的屏幕上添加一个按钮,以便用户选择返回上一个屏幕.我只知道它UIButton是一个子类,UIView我们可以通过使按钮成为该类的子视图来使按钮出现在视图中.以前iOS默认使用谷歌地图MKMapView,我在互联网上看过书中的例子,显示应用程序的屏幕截图,其中按钮或文本框将出现在地图上.但现在只需拖动界面构建器中的按钮就无济于事了.

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

谢谢.

小智 6

在当前视图上构建正常的UI,然后添加GMSMapViewas subview(在index0处)self.view(不要这样做self.view = mapView;)

这是重要的代码:

mapView = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
[self.view insertSubview:mapView atIndex:0];
Run Code Online (Sandbox Code Playgroud)

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

  • 这种方法不起作用.GMSMapView根本不会出现. (2认同)