使用Google Places API为iOS SwiftUI按类型公园查找附近的地方

问题描述

我是Swift / iOS应用开发者的新手。到目前为止,所以我正在努力解决这个问题。基本上,我正在尝试做到这一点,以便在应用打开时(第一个屏幕是地图),它会自动用户当前位置周围找到仅公园的附近地点,并用标记标记这些地点使用更新的SwiftUI / Swift 5.0使用iOS版Google Places API在地图( Google Maps )上:不使用故事板!在这种情况下,表I键入 parks https://developers.google.com/places/web-service/supported_types

到目前为止,这是我拥有的代码...它使用GMSPlaceLikelihood在用户位置附近的地方。这更多是我想要实现的示例,但是改用Google Places API附近的搜索功能,这样我只能显示公园。正在运行的应用图片Image

(然后在附近的位置中找到该地点,如图所示。该列表仅用于显示

在此先感谢您的建议/帮助。

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,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可可豆荚来解析它)。