如何调用另一个函数?

时间:2017-09-17 20:55:43

标签: ios swift google-maps

如何调用已经初始化的函数? 我有一个搜索栏,搜索特定的单词,如果找到它,它会改变一个特定的值。

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

}

我希望我的camera根据我的searchBar.text

更改值
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    print(searchBar.text!)

    if searchBar.text!=="test"{
        mover="test"

    }
}

mover的值已更改,但如何刷新摄像机视图以使其动画到特定坐标。

1 个答案:

答案 0 :(得分:1)

我认为每次你的CLLocationManager给出一个有效的位置时,你的mapView是不好的方法,考虑到你当前的实现,你必须添加一个标志变量来执行didUpdateLocations内的代码只有一次

<强>代码

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

}

之后searchBar文字是&#34;测试&#34;您可以使用.animate(to:)方法将地图设置为新的所需位置

<强>代码

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))
        }
    }
}