MapView Camera在iOS Swift中没有变化

时间:2018-03-29 16:52:22

标签: ios xcode swift3 subview gmsmapview

我很感激我的问题有一些帮助,我正在创建一个名为mapView的视图,并将其类更改为GMSMapView。 但是当我将地图分配给这个mapView时,布局非常好,但默认情况下相机设置为欧洲,但不会改变。

以下是代码:

 @IBOutlet weak var mapView: GMSMapView!
var latitudes = [19.199782,19.19855,19.199179]
var longitudes = [72.8734634,72.872935, 72.874535]
var titles = ["Riviera","Alica","Senate"]
var subTitle = ["1","2","3"]
override func viewDidLoad() {
    super.viewDidLoad()

    let camera = GMSCameraPosition.camera(withLatitude: 19.20060, longitude: 72.8734462, zoom: 15.0)
    let mapView1 = GMSMapView.map(withFrame: CGRect.zero, camera: camera)


    let path = GMSMutablePath()


    for i in 0...2
    {
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: latitudes[i], longitude: longitudes[i])
        marker.title = titles[i]

        marker.snippet = subTitle[i]
        marker.map = mapView1
        path.add(CLLocationCoordinate2D(latitude: latitudes[i], longitude: longitudes[i]))
    }

    let polyLine = GMSPolyline(path: path)

    polyLine.map = mapView1


    mapView = mapView1
    mapView.camera = camera

}

代码的输出是: Output

当我添加地图以查看时,我会得到所需的地图

对上述代码的修改: 而不是mapView = mapView1 put view = mapView1

所需的输出:Desired Output

所以请帮我在屏幕的mapView视图中获取所需的输出。 谢谢!

2 个答案:

答案 0 :(得分:1)

GMSMapView类具有以下功能:

animate(to:GMSCameraPosition)

因此,在您的代码中,不要在mapview上设置相机属性,请执行以下操作:

mapView.animate(to: camera)

希望这有帮助!

修改

仔细查看此代码后,我相信您的mapView1是不必要的,并导致您的插座出现问题。尝试这样的事情:

override func viewDidLoad() {
    super.viewDidLoad()

    let camera = GMSCameraPosition.camera(withLatitude: 19.20060, longitude: 72.8734462, zoom: 15.0)
    let path = GMSMutablePath()

    for i in 0...2
    {
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: latitudes[i], longitude: longitudes[i])
        marker.title = titles[i]

        marker.snippet = subTitle[i]
        marker.map = mapView
        path.add(CLLocationCoordinate2D(latitude: latitudes[i], longitude: longitudes[i]))
    }

    let polyLine = GMSPolyline(path: path)
    polyLine.strokeWidth = 2.0
    polyLine.strokeColor = .black
    polyLine.map = mapView

    mapView.animate(to: camera)
}

在旁注中我还建议用一个字典对象数组替换你的四个数组,每个字典对象包含标题,副标题,纬度和经度,或类似的东西。然后更改for循环以适应

答案 1 :(得分:0)

我建议将视图的类设置为UIView,因为您以编程方式创建了mapView1,然后在初始化mapView1之后

let mapView1 = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

将其作为addSubview(mapView1)添加到视图中。

希望这能帮到你!