简体   繁体   English

如何在swift中检测谷歌地图的摄像头位置

[英]How to detect camera position for google map in swift

I get to know after searching that idleAtCameraPosition and didChangeCameraPosition this methods are called when the map moves . 我在搜索了idleAtCameraPosition和didChangeCameraPosition之后知道了这个方法在地图移动时被调用。 How can i write this method for swift? 我如何为swift编写这个方法? I have set delegate to CLLocationManagerDelegate and GMSMapViewDelegate but still this methods are not called. 我已将委托设置为CLLocationManagerDelegate和GMSMapViewDelegate,但仍未调用此方法。 I have written method as below: 我写的方法如下:

func mapView(mapView: GMSMapView!, didChangeCameraPosition position: GMSCameraPosition!) {
                print(position)
}

func mapView(mapView:GMSMapView!,idleAtCameraPosition position:GMSCameraPosition!)
{
    print(position)

}

But my method is not getting called. 但是我的方法没有被调用。 Also I want to customise info window just like done here : Custom Info Window for Google Maps . 此外,我想自定义信息窗口就像在这里完成: 谷歌地图的自定义信息窗口 But can't find the methods how to do same in swift. 但是在swift中找不到如何做同样的方法。 I am new to swift so not getting this. 我是新手,所以不要这么做。

Maybe too late, but you must extend the class with GMSMapViewDelegate. 可能为时已晚,但您必须使用GMSMapViewDelegate扩展该类。 For example: 例如:

class NearMeViewController: CLLocationManagerDelegate, GMSMapViewDelegate 

Then, how Ztan says, you have to delegate your map with: 然后,Ztan如何说,你必须委托你的地图:

self.mapView.delegate = self

With this, the method didChangeCameraPosition will respond every time that that camera's position is changed. 有了这个,每次相机的位置改变时,方法didChangeCameraPosition都会响应。

Hope it helps. 希望能帮助到你。

You need to import the GMSMapViewDelegate at the top of your class 您需要在类的顶部导入GMSMapViewDelegate

put self.mapView.delegate = self in your viewDidLoad() 在viewDidLoad()中放置self.mapView.delegate = self

and then you can use this method 然后你可以使用这种方法

func mapView(mapView: GMSMapView, idleAtCameraPosition position: GMSCameraPosition) {

        // You can use the position.target for get the current camera position

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM