แสดงแผนที่พื้นฐาน

รูปภาพของ "แสดงแผนที่พื้นฐาน"

ตัวอย่างนี้สร้างแผนที่ที่อยู่ตรงกลางเมืองซีแอตเทิล รัฐวอชิงตัน

เริ่มต้นใช้งาน

คุณต้องกำหนดค่าสภาพแวดล้อมการพัฒนาซอฟต์แวร์ก่อนจึงจะลองใช้โค้ดตัวอย่างได้ ดูข้อมูลเพิ่มเติมได้ที่ตัวอย่างโค้ด Maps SDK สำหรับ 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 สำหรับ iOS พร้อมให้ใช้งานเป็น ไฟล์เก็บถาวรที่ดาวน์โหลดได้ จาก GitHub ทำตามขั้นตอนต่อไปนี้เพื่อติดตั้งและลองใช้แอปตัวอย่าง Maps SDK สำหรับ iOS

  1. เรียกใช้ git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git เพื่อโคลนที่เก็บตัวอย่างลงในไดเรกทอรีในเครื่อง
  2. เปิดหน้าต่างเทอร์มินัล ไปที่ไดเรกทอรีที่คุณโคลนไฟล์ตัวอย่าง แล้ว เจาะลึกลงไปในไดเรกทอรี 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. ในโปรเจ็กต์ Xcode ให้ไปที่ไฟล์ > เพิ่มการอ้างอิงแพ็กเกจ ป้อน https://github.com/googlemaps/ios-maps-sdk เป็น URL กด Enter เพื่อดึงแพ็กเกจ แล้วคลิกเพิ่มแพ็กเกจ
  4. ใน Xcode ให้กดปุ่มคอมไพล์เพื่อ สร้างแอป ด้วย Scheme ปัจจุบัน การสร้างจะทำให้เกิดข้อผิดพลาด ซึ่งจะแจ้งให้คุณป้อนคีย์ API ในไฟล์ SDKConstants.swift สำหรับ Swift หรือSDKDemoAPIKey.h สำหรับ Objective-C
  5. รับคีย์ API จากโปรเจ็กต์ที่เปิดใช้ Maps SDK สำหรับ iOS
  6. แก้ไขไฟล์ SDKConstants.swift สำหรับ Swift หรือไฟล์SDKDemoAPIKey.h สำหรับ Objective-C แล้ววางคีย์ API ลงในการกำหนดค่าของค่าคงที่ apiKey หรือ kAPIKey เช่น

    Swift

    static let apiKey = "YOUR_API_KEY"

    Objective-C

    static NSString *const kAPIKey = @"YOUR_API_KEY";
  7. ในไฟล์ 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.
  8. สร้างและเรียกใช้โปรเจ็กต์ หน้าต่างโปรแกรมจำลอง iOS จะปรากฏขึ้นพร้อมแสดง รายการการสาธิต Maps SDK
  9. เลือกตัวเลือกใดตัวเลือกหนึ่งที่แสดงเพื่อทดลองใช้ฟีเจอร์ของ Maps SDK สำหรับ iOS
  10. หากได้รับข้อความแจ้งให้อนุญาตให้ GoogleMapsDemos เข้าถึงตำแหน่งของคุณ ให้เลือก อนุญาต