Reputation: 81
How to call a function which has already been initialised, again ? I have a search bar which searches for particular word and if it finds it, it changes a particular value.
var mover: String!
@IBOutlet weak var searchBar: UISearchBar!
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let camera = GMSCameraPosition.camera(withLatitude: latPass, longitude: longPass, zoom: 5)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
}
I want my camera
to change value according to my searchBar.text
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
print(searchBar.text!)
if searchBar.text!=="test"{
mover="test"
}
}
The value of mover
is changed, but how to refresh the camera view so it animates to particular coordinates.
Upvotes: 1
Views: 76
Reputation: 20804
I think is bad approach instaciating your mapView every time your CLLocationManager give some valid position, taking in account your current implementation, you must add a flag variable to execute the code inside your didUpdateLocations
only once
Code
var mover: String!
var positionWasUpdated : Bool = false
@IBOutlet weak var searchBar: UISearchBar!
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
guard !positionWasUpdated else{
return
}
let camera = GMSCameraPosition.camera(withLatitude: latPass, longitude: longPass, zoom: 5)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
}
After that when searchBar
text is "test" you can use .animate(to:)
method to animate the map to your new desired location
Code
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
print(searchBar.text!)
if searchBar.text!=="test"{
mover="test"
if let mapView = self.view as? GMSMapView
{
mapView.animate(to: GMSCameraPosition.camera(withLatitude: yourDesiredLatitude,
longitude: yourDesiredLongitude, zoom: 15.0))
}
}
}
Upvotes: 1