如何在不旋转地图的情况下居中放置GoogleMap?

时间:2019-01-19 23:04:23

标签: ios swift google-maps

我在Swift 4中使用Xcode。

在示例中,我将地图上下颠倒(朝北),然后按下按钮以将地图居中到特定位置。但是当我这样做时,地图会自动转回(北方朝上)。

是否可以在不旋转地图的情况下将GoogleMaps定位到某个位置?

import UIKit
import GoogleMaps

class ViewController: UIViewController {
    @IBOutlet weak var mapView: GMSMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.animate(toBearing: 180)
    }

    @IBAction func button(_ sender: Any) {
        let camera = GMSCameraPosition.camera(withLatitude: 1.1111, longitude: 1.1111, zoom: 1)
        mapView.camera = camera
    }
}

2 个答案:

答案 0 :(得分:0)

遵循Apple的文档

rotateEnabled一个布尔值,指示是否使用地图相机的航向信息。

@property(nonatomic,getter = isRotateEnabled)BOOL rotationEnabled讨论当此属性设置为YES且有效的摄影机与地图关联时,摄影机的航向角用于围绕其中心点旋转地图平面。如果将此属性设置为“否”,则相机的航向角将被忽略,并且地图始终处于定向状态,因此,真正的北方位于地图视图的顶部。

按下按钮将其上下颠倒然后更改 isRotateEnabled设置为no,然后将其居中放置地图

答案 1 :(得分:0)

我终于找到了答案,这是可以移动地图而无需旋转地图的代码。

@IBAction func button(_ sender: Any) {
    let camera = GMSCameraPosition.camera(withLatitude: 1.1111, longitude: 1.1111, zoom: mapView.camera.zoom, bearing: mapView.camera.bearing, viewingAngle: 0)
    mapView.camera = camera
}