Menampilkan peta dasar

Gambar Menampilkan peta dasar.

Contoh ini membuat peta yang berpusat di Seattle, Washington.

Mulai

Anda harus mengonfigurasi lingkungan pengembangan Anda sebelum dapat mencoba kode contoh. Untuk mengetahui informasi selengkapnya, lihat Contoh kode Maps SDK for iOS.

Melihat kode

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
      

Menjalankan aplikasi contoh lengkap secara lokal

Aplikasi contoh Maps SDK for iOS tersedia sebagai arsip download dari GitHub. Ikuti langkah-langkah berikut untuk menginstal dan mencoba aplikasi contoh Maps SDK for iOS.

  1. Jalankan git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git untuk meng-clone repositori contoh ke direktori lokal.
  2. Buka jendela terminal, buka direktori tempat Anda meng-clone file sampel, dan buka direktori GoogleMaps:

    Swift

    cd maps-sdk-for-ios-samples/GoogleMaps-Swift
    open GoogleMapsSwiftXCFrameworkDemos.xcodeproj

    Objective-C

    cd maps-sdk-for-ios-samples-main/GoogleMaps
    open GoogleMapsDemos.xcodeproj
  3. Di project Xcode, buka File > Add Package Dependencies. Masukkan https://github.com/googlemaps/ios-maps-sdk sebagai URL, tekan Enter untuk menarik paket, lalu klik Tambahkan Paket.
  4. Di Xcode, tekan tombol kompilasi untuk membangun aplikasi dengan skema saat ini. Build menghasilkan error, yang meminta Anda memasukkan kunci API dalam file SDKConstants.swift untuk Swift atau file SDKDemoAPIKey.h untuk Objective-C.
  5. Dapatkan kunci API dari project Anda dengan Maps SDK for iOS diaktifkan.
  6. Edit file SDKConstants.swift untuk Swift atau file SDKDemoAPIKey.h untuk Objective-C, lalu tempel kunci API Anda ke dalam definisi konstanta apiKey atau kAPIKey. Contoh:

    Swift

    static let apiKey = "YOUR_API_KEY"

    Objective-C

    static NSString *const kAPIKey = @"YOUR_API_KEY";
  7. Dalam file SDKConstants.swift (Swift) atauSDKDemoAPIKey.h (Objective-C), hapus baris berikut, karena digunakan untuk mendaftarkan masalah yang ditentukan pengguna:

    Swift

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

    Objective-C

    #error Register for API Key and insert here.
  8. Bangun dan jalankan proyek. Jendela simulator iOS akan muncul, menampilkan daftar Demo Maps SDK.
  9. Pilih salah satu opsi yang ditampilkan untuk bereksperimen dengan fitur Maps SDK for iOS.
  10. Jika diminta untuk mengizinkan GoogleMapsDemos mengakses lokasi Anda, pilih Izinkan.