What I am trying to accomplish:
open app to see a clear google map view
place a marker on map
if the app is navigated away from(i.e. pressing the home button or double pressing home button to go to different app)
clear all markers on map
Right now the markers are being stored and saved until i open the app again, but i would like to have them cleared if the app is navigated away from.
override func viewDidLoad() {
super.viewDidLoad()
//thought this could possibly clear any previous markers before app fully runs again
self.googleMapView.clear()
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startMonitoringSignificantLocationChanges()
}
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.googleMapView = GMSMapView(frame: self.mapViewContainer.frame)
self.view.addSubview(self.googleMapView)
}
Based from this documentation, you can remove a marker from the map by setting your GMSMarker
's map
property to nil
.
Add this condition at the beginning of the method:
func setuplocationMarker(coordinate: CLLocationCoordinate2D) {
if locationMarker != nil {
locationMarker.map = nil
}
...
}
For reference, you may try this Swift tutorial.