Bermigrasi dari Places SDK for iOS ke Places Swift SDK for iOS (Pratinjau) seharusnya mudah dan dapat dilakukan secara bertahap. Karena struct di Places Swift SDK for iOS (Pratinjau) tidak kompatibel dengan struct berbasis Objective-C, sebaiknya migrasikan bagian fungsi terpisah berdasarkan penggunaan API di GMSPlacesClient.
Menambahkan Places Swift SDK for iOS (Pratinjau) ke project Anda
Langkah-langkah berikut diperlukan untuk menggunakan Places Swift SDK for iOS (Pratinjau):
- Aktifkan Places API (Baru).
Tambahkan Places Swift SDK for iOS (Preview) ke dependensi Anda. Anda dapat memilih untuk menginstal
GooglePlaces
,GooglePlacesSwift
, atau keduanya.Lakukan inisialisasi klien Places dengan
PlacesClient
.
Contoh migrasi langkah demi langkah
Misalnya, aplikasi yang menggunakan Places SDK for iOS menerima saran pelengkapan otomatis berdasarkan input teks, lalu mengambil detail saran tempat pertama. Dengan menggunakan Places SDK for iOS, kode yang ada mungkin terlihat seperti berikut:
// Initialize Places Client.
GMSPlacesClient.provideAPIKey(apiKey)
let client = GMSPlacesClient.shared()
// Fetch Autocomplete Request.
let center = CLLocation(latitude: 37.3913916, longitude: -122.0879074)
let northEast = CLLocationCoordinate2DMake(37.388162, -122.088137)
let southWest = CLLocationCoordinate2DMake(37.395804, -122.077023)
let filter = GMSAutocompleteFilter()
filter.types = [kGMSPlaceTypeRestaurant]
filter.origin = center
filter.locationBias = GMSPlaceRectangularLocationOption(northEast, southWest)
let request = GMSAutocompleteRequest(query: "Sicilian piz")
request.filter = filter
client.fetchAutocompleteSuggestions(from: request) { (results, error) in
guard let results, error == nil else {
print("Autocomplete error: \(String(describing: error))")
return
}
// Fetch Place Request.
guard let placeID = results.first?.placeSuggestion?.placeID else { return }
let myProperties = [GMSPlaceProperty.name, GMSPlaceProperty.website].map {$0.rawValue}
let fetchPlaceRequest = GMSFetchPlaceRequest(placeID: placeID, placeProperties: myProperties, sessionToken: nil)
client.fetchPlace(with: fetchPlaceRequest) { (place: GMSPlace?, error: Error?) in
guard let place, error == nil else { return }
print("Place found: \(String(describing: place.name)); \(String(describing: place.website))")
}
}
Memperbarui penginisialisasi Places Client
Untuk memodernisasi kode dan memanfaatkan kemampuan SDK baru, Anda harus mengganti GMSPlacesClient dengan PlacesClient. Selain itu,
nama parameter diubah dalam metode baru, sehingga Anda harus memperbarui
parameter ke from
, bukan with
. Terakhir, Places Swift SDK for iOS (Pratinjau) menggunakan AutocompleteRequest yang diupgrade.
Kode yang diperbarui
// Initialize Places Swift Client.
let _ = PlacesClient.provideAPIKey(apiKey)
let placesSwiftClient = PlacesClient.shared
Kode asli
// Initialize Places Client.
GMSPlacesClient.provideAPIKey(apiKey)
let client = GMSPlacesClient.shared()
Memperbarui permintaan pelengkapan otomatis
Anda dapat memulai dengan memperbarui alur permintaan pelengkapan otomatis. Kode lama menggunakan callback untuk meminta saran pelengkapan otomatis, sedangkan kode baru menggunakan pola switch
/await
. Callback dapat menambah kompleksitas pada struktur kode dan
penanganan error. Places Swift SDK baru untuk iOS (Pratinjau) mendukung serentak,
yang menyederhanakan operasi asinkron.
// Initialize Places Swift Client.
let _ = PlacesClient.provideAPIKey(apiKey)
let placesSwiftClient = PlacesClient.shared
// Fetch Autocomplete Request.
let center = CLLocation(latitude: 37.3913916, longitude: -122.0879074)
let northEast = CLLocationCoordinate2DMake(37.388162, -122.088137)
let southWest = CLLocationCoordinate2DMake(37.395804, -122.077023)
let bias = RectangularCoordinateRegion(northEast: northEast, southWest: southWest)
let filter = AutocompleteFilter(types: [ .restaurant ], origin: center, coordinateRegionBias: bias)
let autocompleteRequest = AutocompleteRequest(query: "Sicilian piz", filter: filter)
let placeID: String
switch await placesSwiftClient.fetchAutocompleteSuggestions(with: autocompleteRequest) {
case .success(let results):
switch results.first {
case .place(let placeSuggestion):
placeID = placeSuggestion.placeID
case .none:
fallthrough
@unknown default:
return
}
case .failure(let placesError):
print("Autocomplete error: \(placesError)")
return
}
// Initialize Places Client.
GMSPlacesClient.provideAPIKey(apiKey)
let placesClient = GMSPlacesClient.shared()
// Fetch Place Request.
let myProperties = [GMSPlaceProperty.name, GMSPlaceProperty.website].map {$0.rawValue}
let fetchPlaceRequest = GMSFetchPlaceRequest(placeID: placeID, placeProperties: myProperties, sessionToken: nil)
placesClient.fetchPlace(with: fetchPlaceRequest) { (place: GMSPlace?, error: Error?) in
guard let place, error == nil else { return }
print("Place found: \(String(describing: place.name)); \(String(describing: place.website))")
}
Memperbarui nama metode dan class
Terakhir, selesaikan migrasi dengan memfaktorkan ulang kode fetchPlace
dan
menghapus inisialisasi GMSPlacesClient
dan deklarasi
GMSPlaceProperty
. Di Places Swift SDK for iOS (Pratinjau), metode dan nama class
telah diperbarui untuk menghapus awalan "GMS", dan harus diperbarui sebagaimana mestinya;
misalnya, GMSFetchPlaceRequest
menjadi FetchPlaceRequest
.
Penanganan jenis
Metode fetchPlace
baru menggunakan penanganan jenis yang lebih baik. Meskipun kode lama
memerlukan penerusan nilai mentah properti, kode baru tidak mengharuskan
developer mengambil nilai mentah secara eksplisit di sini, sehingga meningkatkan kejelasan dan
keterbacaan.
Serentak
Selain itu, metode baru mendukung konkurensi, yang memungkinkan penggantian
callback di placesClient.fetchPlace
dengan pola switch
/await
di
placesSwiftClient.fetchPlace
.
// Initialize Places Swift Client.
let _ = PlacesClient.provideAPIKey(apiKey)
let placesSwiftClient = PlacesClient.shared
// Fetch Autocomplete Request.
let center = CLLocation(latitude: 37.3913916, longitude: -122.0879074)
let northEast = CLLocationCoordinate2DMake(37.388162, -122.088137)
let southWest = CLLocationCoordinate2DMake(37.395804, -122.077023)
let bias = RectangularCoordinateRegion(northEast: northEast, southWest: southWest)
let filter = AutocompleteFilter(types: [ .restaurant ], origin: center, coordinateRegionBias: bias)
let autocompleteRequest = AutocompleteRequest(query: "Sicilian piz", filter: filter)
let placeID: String
switch await placesSwiftClient.fetchAutocompleteSuggestions(with: autocompleteRequest) {
case .success(let results):
switch results.first {
case .place(let placeSuggestion):
placeID = placeSuggestion.placeID
case .none:
fallthrough
@unknown default:
return
}
case .failure(let placesError):
print("Autocomplete error: \(placesError)")
return
}
// Fetch Place Request.
let fetchPlaceRequest = FetchPlaceRequest(placeID: placeID, placeProperties: [.displayName, .websiteURL])
switch await placesSwiftClient.fetchPlace(with: fetchPlaceRequest) {
case .success(let place):
print("Place found: \(place.displayName): \(String(describing: place.description))")
case .failure(let placesError):
print("Place not found: \(placeID); \(placesError)")
}
Mendapatkan info terbaru
Buka halaman catatan rilis untuk Places Swift SDK for iOS (Pratinjau) guna mempelajari fitur dan perubahan baru.