13

私は iOS プログラミングが初めてで、iOS 用の Google マップ SDK をダウンロードし、Web サイトの指示に従いました (このリンク https://developers.google.com/maps/documentation/ios/startに示されているように ) 。アプリケーションでマップを取得します。

現在、Googleマップの下部にある画面にボタンを追加して、ユーザーが前の画面に戻るオプションを提供しようとしています。

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

#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

最後の 2 行で、ボタンを mapview のサブビューにして前面に表示しようとしたことがわかります。しかし、これは役に立ちませんでした。何が欠けているのか、または他の機能を使用してこれを行う別の方法があるかどうかを教えてください。

私がここでやろうとしていることをよりよく理解できるように、私が作成したストーリーボードのスクリーンショットも確認​​してください。

ストーリーボードのスクリーンショット

ありがとう。

4

5 に答える 5

15

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:@"Button" forState:UIControlStateNormal];
[mapView_ addSubview:button];

100x20ボタンを のサブビューとして追加GMSMapViewし、右下隅に配置します。私はそれをテストしましたが、他のビューと同じようにボタンに触れることができます

編集: また、すべてのコードを から-loadViewに移動します-viewDidLoad-loadViewメソッドは、IB を使用して UI を作成するときに呼び出されることはありません。ドキュメントは次のように-loadView述べています。

Interface Builder を使用してビューを作成し、View Controller を初期化する場合は、このメソッドをオーバーライドしてはなりません。

self.view編集 2: Interface Builder を使用してビュー階層を作成する場合、あなたが行っているようにプロパティをリセットすることはできません。

あなたの-viewDidLoad

[self.view addSubview: mapView_];

それ以外の

self.view = mapView_;

GMSMapViewプロパティに渡す場合self.view、マップはこの時点からコントローラーにあるビューのみです。それが、IB で作成されたボタンが表示されない理由だと思います。

于 2013-05-20T07:45:35.523 に答える
3

UIViewController私は同じ問題に直面しましたが、修正は非常に簡単です。このメソッドをオーバーライドするだけで、作成ロジックをそのメソッド内に
-(void) viewWillAppear:(BOOL)animated配置できます。UIButton

例:

-(void) viewWillAppear:(BOOL)animated
{
 locationButton = [UIButton buttonWithType:UIButtonTypeCustom];
 locationButton.frame = CGRectMake(0, 30, self.view.frame.size.width/6, self.view.frame.size.height/6);
 [locationButton setImage:[UIImage imageNamed:@"location_enabled.png"] forState:UIControlStateNormal];
 [self.view addSubview:locationButton];
}
于 2015-04-19T16:48:58.197 に答える
3

通常、InterfaceBuilder でビューを作成し、mapView を 0 index subview に配置します。

mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
mapView_.myLocationEnabled = YES;
[self.view insertSubview:mapView_ atIndex:0];

このスレッドの Raul Clauss に感謝します

また、mapView のサイズをカスタマイズして、次のように変更できます。

mapView_ = [GMSMapView mapWithFrame:_mapHolder.bounds camera:camera];
mapView_.myLocationEnabled = YES;
[_mapHolder insertSubview:mapView_ atIndex:0];

mapHolder は、IB 内で作成された UIView です。

于 2013-11-30T20:31:57.010 に答える