การตั้งค่า Google Maps
ขั้นตอนการใช้งาน Google Maps SDK สำหรับ iOS ผ่าน MapConductor เลือกวิธีนี้สำหรับแอปที่มีการพึ่งพา Google Maps อยู่แล้ว หรือเมื่อต้องการใช้ GoogleMapDesign map type
ก่อนเริ่ม
รับคีย์ API
- เข้าถึง Google Cloud Console
- สร้างโปรเจกต์หรือเลือกโปรเจกต์ที่มีอยู่
- เปิดใช้งาน Maps SDK สำหรับ iOS
- สร้างคีย์ API จาก 'Credentials' (ข้อมูลรับรอง)
- จำกัดคีย์ด้วย Bundle ID ของแอป
เพิ่มด้วย Swift Package Manager
ป้อน URL ของแพ็กเกจที่ Xcode > File > Add Package Dependencies SDK เนทีฟจะถูกเพิ่มเป็น dependency โดยอัตโนมัติ จึงไม่จำเป็นต้องเพิ่มแยกต่างหาก
https://github.com/MapConductor/ios-for-googlemaps
dependencies: [
.package(url: "https://github.com/MapConductor/ios-sdk-core", from: "1.3.1"),
.package(url: "https://github.com/MapConductor/ios-for-googlemaps", from: "1.0.0"),
],
targets: [
.target(
name: "YourApp",
dependencies: [
.product(name: "MapConductorCore", package: "ios-sdk-core"),
.product(name: "MapConductorForGoogleMaps", package: "ios-for-googlemaps"),
]
),
]เริ่มต้นใช้งาน SDK
เรียก GMSServices.provideAPIKey(_:) ครั้งเดียวก่อนที่มุมมองแผนที่จะถูกสร้าง โครงสร้าง App ที่ @main คือตำแหน่งที่แนะนำ
import SwiftUI
import GoogleMaps
import MapConductorForGoogleMaps
@main
struct MyApp: App {
init() {
GMSServices.provideAPIKey("YOUR_GOOGLE_MAPS_API_KEY")
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}// Alternative: call it from the view's sdkInitialize closure
GoogleMapView(state: mapState, sdkInitialize: {
GMSServices.provideAPIKey("YOUR_GOOGLE_MAPS_API_KEY")
})เพิ่มสิทธิ์ตำแหน่งที่ตั้ง (หากจำเป็น)
หากใช้บริการตำแหน่งใน Google Maps ให้ระบุวัตถุประสงค์ใน Info.plist และขออนุญาตจากแอป
<key>NSLocationWhenInUseUsageDescription</key> <string>We need your location to display on the map</string>
import CoreLocation
final class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
private let manager = CLLocationManager()
override init() {
super.init()
manager.delegate = self
}
func requestPermission() {
manager.requestWhenInUseAuthorization()
}
}ตรวจสอบ
สิ่งที่ควรเห็น
Build และรัน ContentView นี้ หากแผนที่ปรากฏและซูมหรือเลื่อนแผนที่ได้ ถือว่าเสร็จสิ้น
import SwiftUI
import MapConductorCore
import MapConductorForGoogleMaps
struct ContentView: View {
@StateObject var mapState = GoogleMapViewState(
cameraPosition: MapCameraPosition(
position: GeoPoint(latitude: 35.6762, longitude: 139.6503),
zoom: 12.0
)
)
var body: some View {
GoogleMapView(state: mapState)
.ignoresSafeArea()
}
}ดีไซน์แผนที่
GoogleMapDesign รองรับประเภทแผนที่ของ Google
การแก้ปัญหา
แผนที่ไม่แสดง (หน้าจอสีเทา)
- ตรวจสอบว่าได้เรียก GMSServices.provideAPIKey(_:) ก่อนสร้างมุมมองแผนที่หรือไม่
- ตรวจสอบใน Console ว่าเปิดใช้งาน Maps SDK สำหรับ iOS หรือไม่
- ตรวจสอบว่าข้อจำกัด Bundle ID ของคีย์ตรงกับ Bundle ID จริงหรือไม่
- ตรวจสอบว่า Deployment Target เป็น iOS 16.0 ขึ้นไปหรือไม่
Build ไม่ผ่าน
- ล้างโฟลเดอร์ build (Cmd+Shift+K)
- ลบ ~/Library/Developer/Xcode/DerivedData แล้ว build ใหม่
- อัปเดต dependencies ของ Swift Package Manager หลีกเลี่ยงการใช้ร่วมกับ CocoaPods
ถัดไป
เมื่อ GoogleMapView ปรากฏขึ้น คุณสามารถเขียน Marker, Polyline และ Circle ด้วย API ที่ใช้ร่วมกันได้ แม้จะเปลี่ยนผู้ให้บริการ ส่วนนี้ก็ไม่ต้องเขียนใหม่