基本地図を表示する

基本地図を表示する画像

この例では、ワシントン州シアトルを中心とする地図を作成します。

使ってみる

サンプルコードを試す前に、開発環境を構成する必要があります。 詳しくは、Maps SDK for iOS のコードサンプルをご覧ください。

コードを表示する

Swift

import GoogleMaps import UIKit  class BasicMapViewController: UIViewController {   var statusLabel: UILabel!    override func viewDidLoad() {     super.viewDidLoad()      // Seattle coordinates     let camera = GMSCameraPosition(latitude: 47.6089945, longitude: -122.3410462, zoom: 14)     let mapView = GMSMapView(frame: view.bounds, camera: camera)     mapView.delegate = self     view = mapView     navigationController?.navigationBar.isTranslucent = false      statusLabel = UILabel(frame: .zero)     statusLabel.alpha = 0.0     statusLabel.backgroundColor = .blue     statusLabel.textColor = .white     statusLabel.textAlignment = .center     view.addSubview(statusLabel)     statusLabel.translatesAutoresizingMaskIntoConstraints = false     NSLayoutConstraint.activate([       statusLabel.topAnchor.constraint(equalTo: view.topAnchor),       statusLabel.heightAnchor.constraint(equalToConstant: 30),       statusLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor),       statusLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor),     ])   } }  extension BasicMapViewController: GMSMapViewDelegate {   func mapViewDidStartTileRendering(_ mapView: GMSMapView) {     statusLabel.alpha = 0.8     statusLabel.text = "Rendering"   }    func mapViewDidFinishTileRendering(_ mapView: GMSMapView) {     statusLabel.alpha = 0.0   } }       

Objective-C

#import "GoogleMapsDemos/Samples/BasicMapViewController.h"  #import <GoogleMaps/GoogleMaps.h>  @implementation BasicMapViewController {   UILabel *_statusLabel; }  - (void)viewDidLoad {   [super viewDidLoad];   // Seattle coordinates   GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:47.6089945                                                           longitude:-122.3410462                                                                zoom:14];   GMSMapView *view = [GMSMapView mapWithFrame:CGRectZero camera:camera];   view.delegate = self;   self.view = view;    // Add status label, initially hidden.   _statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];   _statusLabel.alpha = 0.0f;   _statusLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;   _statusLabel.backgroundColor = [UIColor blueColor];   _statusLabel.textColor = [UIColor whiteColor];   _statusLabel.textAlignment = NSTextAlignmentCenter;    [view addSubview:_statusLabel]; }  - (void)mapViewDidStartTileRendering:(GMSMapView *)mapView {   _statusLabel.alpha = 0.8f;   _statusLabel.text = @"Rendering"; }  - (void)mapViewDidFinishTileRendering:(GMSMapView *)mapView {   _statusLabel.alpha = 0.0f; }  @end       

完全なサンプルアプリをローカルで実行する

Maps SDK for iOS のサンプルアプリは、GitHub からダウンロード アーカイブとして入手できます。以下の手順に沿って、Maps SDK for iOS のサンプルアプリをインストールして試します。

  1. git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git を実行して、サンプル リポジトリのクローンをローカル ディレクトリに作成します。
  2. ターミナル ウィンドウを開き、サンプルファイルのクローンを作成したディレクトリに移動して、GoogleMaps ディレクトリにドリルダウンします。

    Swift

    cd maps-sdk-for-ios-samples-main/GoogleMaps-Swift pod install open GoogleMapsSwiftDemos.xcworkspace

    Objective-C

    cd maps-sdk-for-ios-samples-main/GoogleMaps pod install open GoogleMapsDemos.xcworkspace
  3. Xcode でコンパイル ボタンを押して、現在のスキームでアプリをビルドします。ビルドでエラーが発生し、Swift の場合は SDKConstants.swift ファイル、Objective-C の場合は SDKDemoAPIKey.h ファイルに API キーを入力するよう求められます。
  4. Maps SDK for iOS を有効にしたプロジェクトからAPI キーを取得します。
  5. Swift の場合は SDKConstants.swift ファイルを、Objective-C の場合は SDKDemoAPIKey.h ファイルを編集し、apiKey 定数または kAPIKey 定数の定義に API キーを貼り付けます。次に例を示します。

    Swift

    static let apiKey = "YOUR_API_KEY"

    Objective-C

    static NSString *const kAPIKey = @"YOUR_API_KEY";
  6. SDKConstants.swift ファイル(Swift)または SDKDemoAPIKey.h ファイル(Objective-C)で、次の行を削除します。この行は、ユーザー定義の問題の登録に使用されます。

    Swift

    #error (Register for API Key and insert here. Then delete this line.)

    Objective-C

    #error Register for API Key and insert here.
  7. プロジェクトをビルドして実行します。iOS シミュレータ ウィンドウが表示され、Maps SDK のデモのリストが表示されます。
  8. 表示されたオプションのいずれかを選択して、Maps SDK for iOS の機能を試します。
  9. GoogleMapsDemos に現在地へのアクセスを許可するよう求められたら、[許可] を選択します。