
เริ่มต้นใช้งาน
คุณต้องกำหนดค่าสภาพแวดล้อมการพัฒนาซอฟต์แวร์ก่อนจึงจะลองใช้โค้ดตัวอย่างได้ ดูข้อมูลเพิ่มเติมได้ที่ตัวอย่างโค้ด 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
- เรียกใช้
git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.gitเพื่อโคลนที่เก็บตัวอย่างลงในไดเรกทอรีในเครื่อง เปิดหน้าต่างเทอร์มินัล ไปที่ไดเรกทอรีที่คุณโคลนไฟล์ตัวอย่าง แล้ว เจาะลึกลงไปในไดเรกทอรี GoogleMaps
Swift
cd maps-sdk-for-ios-samples/GoogleMaps-Swift
open GoogleMapsSwiftXCFrameworkDemos.xcodeprojObjective-C
cd maps-sdk-for-ios-samples-main/GoogleMaps
open GoogleMapsDemos.xcodeproj- ในโปรเจ็กต์ Xcode ให้ไปที่ไฟล์ > เพิ่มการอ้างอิงแพ็กเกจ
ป้อน
https://github.com/googlemaps/ios-maps-sdkเป็น URL กด Enter เพื่อดึงแพ็กเกจ แล้วคลิกเพิ่มแพ็กเกจ - ใน Xcode ให้กดปุ่มคอมไพล์เพื่อ
สร้างแอป
ด้วย Scheme ปัจจุบัน การสร้างจะทำให้เกิดข้อผิดพลาด ซึ่งจะแจ้งให้คุณป้อนคีย์ API
ในไฟล์
SDKConstants.swiftสำหรับ Swift หรือSDKDemoAPIKey.hสำหรับ Objective-C - รับคีย์ API จากโปรเจ็กต์ที่เปิดใช้ Maps SDK สำหรับ iOS
- แก้ไขไฟล์
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";
- ในไฟล์
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.
- สร้างและเรียกใช้โปรเจ็กต์ หน้าต่างโปรแกรมจำลอง iOS จะปรากฏขึ้นพร้อมแสดง รายการการสาธิต Maps SDK
- เลือกตัวเลือกใดตัวเลือกหนึ่งที่แสดงเพื่อทดลองใช้ฟีเจอร์ของ Maps SDK สำหรับ iOS
- หากได้รับข้อความแจ้งให้อนุญาตให้ GoogleMapsDemos เข้าถึงตำแหน่งของคุณ ให้เลือก อนุญาต