I have a map view which has a button that, when pressed, should center the map on the user's current location. I am trying to achieve this using Swift's Combine framework. I tried solving this by adding a @State
property called mapCenter
and assigning to this property in Combine's assign(to:on:)
subject, as follows:
struct MapWithButtonView: View {
// What the current map view center should be.
@State var mapCenter = CLLocationCoordinate2D(latitude: 42.35843, longitude: -71.05977)
// A subject whose `send(_:)` method is being called from within the CenterButton view to center the map on the user's location.
private var centerButtonTappedPublisher = PassthroughSubject<Bool, Never>()
// A publisher that turns a "center button tapped" event into a coordinate.
private var centerButtonTappedCoordinatePublisher: AnyPublisher<CLLocationCoordinate2D?, Never> {
centerButtonTappedPublisher
.map { _ in LocationManager.default.currentUserCoordinate }
.eraseToAnyPublisher()
}
private var coordinatePublisher: AnyPublisher<CLLocationCoordinate2D, Never> {
Publishers.Merge(LocationManager.default.$initialUserCoordinate, centerButtonTappedCoordinatePublisher)
.replaceNil(with: CLLocationCoordinate2D(latitude: 42.35843, longitude: -71.05977))
.eraseToAnyPublisher()
}
private var cancellableSet: Set<AnyCancellable> = []
init() {
// This does not result in an update to the view... why not?
coordinatePublisher
.receive(on: RunLoop.main)
.handleEvents(receiveSubscription: { (subscription) in
print("Receive subscription")
}, receiveOutput: { output in
print("Received output: \(String(describing: output))")
}, receiveCompletion: { _ in
print("Receive completion")
}, receiveCancel: {
print("Receive cancel")
}, receiveRequest: { demand in
print("Receive request: \(demand)")
})
.assign(to: \.mapCenter, on: self)
.store(in: &cancellableSet)
}
var body: some View {
ZStack {
MapView(coordinate: mapCenter)
.edgesIgnoringSafeArea(.all)
CenterButton(buttonTappedPublisher: centerButtonTappedPublisher)
}
}
}
The MapView
is a UIViewRepresentable
view and looks like this:
struct MapView: UIViewRepresentable {
// The center of the map.
var coordinate: CLLocationCoordinate2D
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView(frame: .zero)
mapView.showsUserLocation = true
return mapView
}
func updateUIView(_ view: MKMapView, context: Context) {
let span = MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)
let region = MKCoordinateRegion(center: coordinate, span: span)
view.setRegion(region, animated: true)
}
}
The CenterButton
is a simple button that looks like this:
struct CenterButton: View {
var buttonTappedPublisher: PassthroughSubject<Bool, Never>
var body: some View {
Button(action: {
self.buttonTappedPublisher.send(true)
}) {
Image(systemName: "location.fill")
.imageScale(.large)
.accessibility(label: Text("Center map"))
}
}
}
And the LocationManager
is an ObservableObject
which publishes the user's current and initial location:
class LocationManager: NSObject, ObservableObject {
// The first location reported by the CLLocationManager.
@Published var initialUserCoordinate: CLLocationCoordinate2D?
// The latest location reported by the CLLocationManager.
@Published var currentUserCoordinate: CLLocationCoordinate2D?
private let locationManager = CLLocationManager()
static let `default` = LocationManager()
private override init() {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.pausesLocationUpdatesAutomatically = true
locationManager.activityType = .other
locationManager.requestWhenInUseAuthorization()
}
}
extension LocationManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .authorizedAlways, .authorizedWhenInUse:
NSLog("Location authorization status changed to '\(status == .authorizedAlways ? "authorizedAlways" : "authorizedWhenInUse")'")
enableLocationServices()
case .denied, .restricted:
NSLog("Location authorization status changed to '\(status == .denied ? "denied" : "restricted")'")
disableLocationServices()
case .notDetermined:
NSLog("Location authorization status changed to 'notDetermined'")
default:
NSLog("Location authorization status changed to unknown status '\(status)'")
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// We are only interested in the user's most recent location.
guard let location = locations.last else { return }
// Use the location to update the location manager's published state.
let coordinate = location.coordinate
if initialUserCoordinate == nil {
initialUserCoordinate = coordinate
}
currentUserCoordinate = coordinate
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
NSLog("Location manager failed with error: \(error)")
}
// MARK: Helpers.
private func enableLocationServices() {
locationManager.startUpdatingLocation()
}
private func disableLocationServices() {
locationManager.stopUpdatingLocation()
}
}
Unfortunately, the above does not work. The view is never updated when the CenterButton
is tapped. I ended up solving this problem by using an ObservableObject
view model object with a @Published var mapCenter
property, however I don't know why my initial solution using @State
does not work. What is wrong with updating @State
as I have done above?
Note that if trying to reproduce this, you will need to add the NSLocationWhenInUseUsageDescription
key with a value such as "This app needs access to your location" in your Info.plist
file in order to be able to grant location permissions.
NEW ANSWER:
ok, i am a combine newbie but it didn't let me go, so i tried and tried...and now it works, even in simulator.
the problem was, that you did all in the struct/Contentview instead of doing the combine things in a seperate class which publishes the value you want to change.
check this out:
OLD ANSWER:
ok, because you don't give us a copyable reproducable example, i made an easy example which works. Just feel free to copy and use it for your problem.