Reputation: 76
I am building a new app and am trying to use the new Google Places Swift SDK for iOS. I am following these guides provided by Google: Google Setup Guide and Google Text Search Guide
Unfortunately, I always get the following error:
The main parts of my code look like this and are setup inside a Apple App Project:
// my content view
import SwiftUI
import GooglePlacesSwift
@main
struct MyApp: App {
init() {
PlacesClient.provideAPIKey("my API key")
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
// my google file
import Foundation
import SwiftUI
import CoreLocation
import GooglePlacesSwift
func searchForPizzaInNewYork() async {
// Define the bounds for New York City
guard let region = RectangularCoordinateRegion(
northEast: CLLocationCoordinate2D(latitude: 20, longitude: 30),
southWest: CLLocationCoordinate2D(latitude: 40, longitude: 50)
) else {
print("Failed to create region.")
return
}
// Create a SearchByTextRequest with the desired parameters
let searchByTextRequest = SearchByTextRequest(
textQuery: "pizza",
placeProperties: [.placeID],
locationBias: region,
includedType: .restaurant,
maxResultCount: 2,
isStrictTypeFiltering: true
)
// Perform the search
switch await PlacesClient.shared.searchByText(with: searchByTextRequest) {
case .success(let places):
// Handle the places retrieved
for place in places {
if let placeID = place.placeID {
print("Place ID: \(placeID)")
}
}
case .failure(let placesError):
// Handle the error with detailed feedback
print("Error fetching places: \(placesError.localizedDescription)")
// Print additional details if available
print("Error details: \(placesError)")
if let nsError = placesError as NSError? {
print("Error code: \(nsError.code)")
print("Error domain: \(nsError.domain)")
print("User info: \(nsError.userInfo)")
}
}
}
// my view
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
Button(action: {
Task {
await searchForPizzaInNewYork()
}
}) {
Text("Search for Pizza in New York")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
}
.padding()
}
.padding()
}
}
Upvotes: 0
Views: 90