使用 Google 个地点 API 为 iOS SwiftUI 按类型(公园)查找附近的地点

Using Google Places API to find nearby places BY TYPE (parks) for iOS SwiftUI

我是 Swift/iOS 应用程序开发的新手。到目前为止,所以我正在努力解决这个问题。基本上,我试图做到这一点,当应用程序打开时(第一个屏幕是地图),它会自动找到附近的地方,这些地方只是用户当前位置周围的公园,并且有这些位置使用 Google Places API for iOS using updated SwiftUI/Swift 5.0 在地图 (Google Maps) 上用标记进行注释: 不使用故事板!。 Table 我输入,在这种情况下,parkshttps://developers.google.com/places/web-service/supported_types

到目前为止,这是我拥有的代码...它使用用户位置附近地点的 GMSPlaceLikelihood。这更像是我想要实现的一个例子,但是使用 Google Places API 附近的搜索来代替,所以我只能显示公园。应用程序图像 运行: Image

(从附近找到的地方然后在 table 中列出,如图所示。此列表仅供展示)

提前感谢任何advice/help。

GoogleMapsView.swift:

@ObservedObject var locationManager = LocationManager()
@ObservedObject var place = PlacesManager()

func makeUIView(context: Self.Context) -> GMSMapView {
    let camera = GMSCameraPosition.camera(withLatitude: locationManager.latitude, longitude: locationManager.longitude, zoom: 14)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    mapView.isMyLocationEnabled = true
    mapView.settings.rotateGestures = false
    mapView.settings.tiltGestures = false
    mapView.isIndoorEnabled = false
    mapView.isTrafficEnabled = false
    mapView.isBuildingsEnabled = false
    mapView.settings.myLocationButton = true
    
    place.currentPlacesList(completion: { placeLikelihoodList in
        if let placeLikelihoodList = placeLikelihoodList {
            print("total places: \(placeLikelihoodList.count)")
            
            for likelihood in placeLikelihoodList {
                let place = likelihood.place
                let position = CLLocationCoordinate2D(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
                let marker = GMSMarker(position: position)
                marker.title = place.name
                marker.map = mapView
            }
        }
    })
    
    return mapView
}

func updateUIView(_ mapView: GMSMapView, context: Context) {
    //        let camera = GMSCameraPosition.camera(withLatitude: locationManager.latitude, longitude: locationManager.longitude, zoom: zoom)
    //        mapView.camera = camera
    mapView.animate(toLocation: CLLocationCoordinate2D(latitude: locationManager.latitude, longitude: locationManager.longitude))
}

PlacesManager.swift:

class PlacesManager: NSObject, ObservableObject {

private var placesClient = GMSPlacesClient.shared()

@Published var places = [GMSPlaceLikelihood]()

override init() {
    super.init()
    currentPlacesList { (places) in
        guard let places = places else {
            return
        }
        self.places = places
    }
}

func currentPlacesList(completion: @escaping (([GMSPlaceLikelihood]?) -> Void)) {
    // Specify the place data types to return.
    let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) |
        UInt(GMSPlaceField.placeID.rawValue) | UInt(GMSPlaceField.types.rawValue) | UInt(GMSPlaceField.coordinate.rawValue))!
    placesClient.findPlaceLikelihoodsFromCurrentLocation(withPlaceFields: fields, callback: {
        (placeLikelihoodList: Array<GMSPlaceLikelihood>?, error: Error?) in
        
        if let error = error {
            print("An error occurred: \(error.localizedDescription)")
            return
        }
        
        if let placeLikelihoodList = placeLikelihoodList {
            for likelihood in placeLikelihoodList {
                let place = likelihood.place
            }
            
            completion(placeLikelihoodList)
        }
    })
}

ContentView.swift:

var body: some View {
    VStack {
        GoogleMapsView()
            .edgesIgnoringSafeArea(.top)
            .frame(height: 400)
        PlacesList()
    }
    .offset(y: 100)
}

https://developers.google.com/places/web-service/search

我建议您访问此页面,它详细描述了 API 并显示了如何使用不同参数调用它的示例。

收到响应后(我建议以 json 格式获取它并使用 SwiftyJSON cocoa pod 来解析它)填充 table.