[英]Having trouble with ObserveObject Array changes in my code
目前,我的代碼在用戶當前位置附近找到附近的公園,並在控制台中輸出名稱/位置很好(使用 Google Maps & Google Places API)。 控制台輸出。 我要在我的應用程序中嘗試完成的下一步是在返回 mapView 之前,在 GoogleMapsView.swift 文件中使用標記對地圖上的每個公園進行注釋。 我的問題是在找到公園位置后我不知道如何做到這一點。 所以我的問題是如何觀察陣列中的公園位置變化,以便之后能夠在地圖上標記它們? 這是我的代碼。 在我的 ContentView.swift 中,我只是將 GoogleMapsView 傳遞到視圖中。
PlacesManager.swift:
import GooglePlaces
class PlacesManager: NSObject, ObservableObject, LocationManagerDelegate {
@Published var places = [Place]()
lazy var googleClient: GoogleClientRequest = GoogleClient()
var locationManager: LocationManager?
var searchRadius : Int = 700
override init() {
super.init()
load()
}
func load() {
locationManager = LocationManager()
locationManager?.delegate = self
}
func locationDidChange(location: CLLocation) {
let lat = location.coordinate.latitude
let long = location.coordinate.longitude
let newLocation: CLLocation = CLLocation(latitude: lat, longitude: long)
fetchGoogleData(forLocation: newLocation, searchRadius: 700)
}
func fetchGoogleData(forLocation: CLLocation, searchRadius: Int) {
//guard let location = currentLocation else { return }
googleClient.getGooglePlacesData(location: forLocation, withinMeters: searchRadius) { (response) in
self.places = response.results
}
}
//... print parks in console code is here
}
LocationManager.swift:
protocol LocationManagerDelegate: class {
func locationDidChange(location: CLLocation)
}
class LocationManager: NSObject, ObservableObject {
weak var delegate: LocationManagerDelegate?
private let locationManager = CLLocationManager()
@Published var location: CLLocation? {
willSet { objectWillChange.send() }
}
var latitude: CLLocationDegrees {
return location?.coordinate.latitude ?? 0
}
var longitude: CLLocationDegrees {
return location?.coordinate.longitude ?? 0
}
override init() {
super.init()
let status = CLLocationManager.authorizationStatus()
if status == .notDetermined {
locationManager.requestWhenInUseAuthorization()
}
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
}
extension LocationManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager,didChangeAuthorization status: CLAuthorizationStatus) {
guard status == .authorizedWhenInUse else {
return
}
locationManager.requestLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else {
return
}
self.location = location
self.delegate?.locationDidChange(location: location)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
}
GoogleMapsView.swift:
struct GoogleMapsView: UIViewRepresentable {
@ObservedObject var locationManager = LocationManager()
@ObservedObject var place: PlacesManager = PlacesManager()
func makeUIView(context: Self.Context) -> GMSMapView {
let camera = GMSCameraPosition.camera(withLatitude: locationManager.latitude, longitude: locationManager.longitude, zoom: 15)
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
return mapView
}
}
在我的項目中,我使用 UIKit 來處理地圖和標記,該方法與您發布的方法完全不同。 無論如何,這是我從Place API更新標記后用來“刷新”地圖的功能之一,希望它有所幫助~
func addMarkerFrom(places: [Place]) {
for place in places {
if place.user_ratings_total ?? 100 > UInt(100) && place.business_status == "OPERATIONAL" {
if place.types?.contains(where: placeTypesFilter.contains) == false {
DispatchQueue.main.async {
let marker = GMSMarker()
let fetchedPlace = place
let markerIconImage = UIImage(named: "Marker_icon_blue")
marker.position = CLLocationCoordinate2D(latitude: (fetchedPlace.geometry?.location?.latitude)!, longitude: (fetchedPlace.geometry?.location?.longitude)!)
marker.title = place.name
marker.snippet = "\(String(describing: fetchedPlace.user_ratings_total)), \(String(describing: fetchedPlace.rating))"
marker.appearAnimation = .pop
marker.icon = self.drawMarkerWith(image: markerIconImage!, rating: fetchedPlace.rating!, userRatingTool: fetchedPlace.user_ratings_total!)
marker.infoWindowAnchor = CGPoint(x: 0.5, y: 2.3)
marker.tracksInfoWindowChanges = true
marker.map = self.mapView
filteredPlaces.append(fetchedPlace)
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.