如何自定义Google Maps for iOS Swift的“myLocationButton”(How to customize “myLocationButton” of Google Maps for iOS Swift)

我是iOS开发的新手。 我想创建一个自定义按钮,其功能与Google Maps SDK for iOS中的默认按钮( myLocationButton )相同。 我可以这样做吗? 我可以创建按钮,但我不知道如何添加动作来跟踪我的当前位置并在屏幕中心显示标记。 我正在使用Swift 3。

任何帮助,将不胜感激!

I'm a newbie in iOS development. I want to create a custom button with the same functionality as the default button (myLocationButton) in Google Maps SDK for iOS. Can I do that? I can create the button but I don't know how to add action to track my current location and show the marker in the center of the screen. I'm using Swift 3.

Any help would be appreciated!

最满意答案

您可以像这样实现操作:

func gotoMyLocationAction(sender: UIButton) { guard let lat = self.mapView.myLocation?.coordinate.latitude, let lng = self.mapView.myLocation?.coordinate.longitude else { return } let camera = GMSCameraPosition.camera(withLatitude: lat ,longitude: lng , zoom: zoom) self.mapView.animate(to: camera) }

如果它们存在,则将您的地图居中放置到当前位置。 希望有所帮助。

确保您的GMSMapView上的位置检测已启用

self.mapView.isMyLocationEnabled = true

You could implement the action like so:

func gotoMyLocationAction(sender: UIButton) { guard let lat = self.mapView.myLocation?.coordinate.latitude, let lng = self.mapView.myLocation?.coordinate.longitude else { return } let camera = GMSCameraPosition.camera(withLatitude: lat ,longitude: lng , zoom: zoom) self.mapView.animate(to: camera) }

This center your map to your current position if they exists. Hope that helps.

Be sure the position detection on your GMSMapView is enabled

self.mapView.isMyLocationEnabled = true

更多推荐