如何自定义Google Maps for iOS Swift的"myLocationButton"

use*_*own 5 ios google-maps-sdk-ios swift

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

- 这是图像 -

任何帮助,将不胜感激!

Tho*_* G. 9

您可以像这样实现操作:

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)
}
Run Code Online (Sandbox Code Playgroud)

如果地图存在,则将地图置于当前位置的中心位置.希望有所帮助.

确保GMSMapView启用了位置检测功能

self.mapView.isMyLocationEnabled = true
Run Code Online (Sandbox Code Playgroud)


小智 5

使用 GoogleMapSDK 2.7.0 更新 @Thomas 答案的 Swift 4

func didTapMyLocationButton(for mapView: GMSMapView) -> Bool {

    guard let lat = googleMapView.myLocation?.coordinate.latitude,
        let lng = googleMapView.myLocation?.coordinate.longitude else { return false }

    let camera = GMSCameraPosition.camera(withLatitude: lat ,longitude: lng , zoom: userMapZoom)
    googleMapView.animate(to: camera)

    return true

}
Run Code Online (Sandbox Code Playgroud)