Swift GoogleMaps fitBounds Zoom

3

新手程序员试图将GoogleMap放入我的视图中。

我已经搜索了很多信息,得出了这个结论,但对我来说并没有起作用。

覆盖函数loadView() {

    var markerList = [GMSMarker]()

    // Create a GMSCameraPosition
    let camera = GMSCameraPosition.camera(withLatitude: 4.390205, longitude: 2.154007, zoom: 8)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    mapView.isMyLocationEnabled = true
    view = mapView

    mapView.settings.myLocationButton = true
    //mapView.setMinZoom(10, maxZoom: 20)

    //create markers
    for loc in arrayOfMapStops {
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: loc.lat, longitude: loc.long)
        marker.title = loc.address
        marker.snippet = loc.type
        if loc.type == "Entrega" {marker.icon = GMSMarker.markerImage(with: .green)}
        else {marker.icon = GMSMarker.markerImage(with: .blue)}
        marker.map = mapView
        markerList.append(marker)
    }

    //fit map to markers
    var bounds = GMSCoordinateBounds()
    for marker in markerList {
        bounds = bounds.includingCoordinate(marker.position)
    }
    let update = GMSCameraUpdate.fit(bounds)
    mapView.moveCamera(update)
}

地图的缩放比例不正确。 情况图片 有人可以帮我解决缩放问题吗?
谢谢! :)

感谢@Nrzonline的建议! - Raul F.
2个回答

5

我自己解决了问题。我使用了DispatchQueue来设置地图的正确缩放。

这是我的最终代码:

override func loadView() {

    var markerList = [GMSMarker]()

    // Create a GMSCameraPosition
    let camera = GMSCameraPosition.camera(withLatitude: 40.4167 , longitude: -3.70325, zoom: 8)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    mapView.isMyLocationEnabled = true
    view = mapView

    mapView.settings.myLocationButton = true
    //mapView.setMinZoom(10, maxZoom: 20)

    //create markers
    for loc in arrayOfMapStops {
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: loc.lat, longitude: loc.long)
        marker.title = loc.address
        marker.snippet = loc.type
        if loc.type == "Entrega" {marker.icon = GMSMarker.markerImage(with: .green)}
        else {marker.icon = GMSMarker.markerImage(with: .blue)}
        marker.map = mapView
        markerList.append(marker)
    }

    delay(seconds: 3) { () -> () in
        //fit map to markers
        var bounds = GMSCoordinateBounds()
        for marker in markerList {
            bounds = bounds.includingCoordinate(marker.position)
        }
        let update = GMSCameraUpdate.fit(bounds, withPadding: 100.0)
        mapView.animate(with: update)
    }
}

func delay(seconds: Double, completion:@escaping ()->()) {
    let when = DispatchTime.now() + seconds
    DispatchQueue.main.asyncAfter(deadline: when) {
        completion()
    }
}

:)


对我来说没有用,我仍然面临着相同的问题,有人可以帮忙吗? - ANeme
@ANeme 你用了DispatchQueue吗?对我来说,它像魔法一样好用。 - Raul F.
我找到了问题所在,你的dispatchQueue确实帮助动画按预期工作,但我的问题是每次需要包含坐标时都必须将其重新分配给bounds :) 这就是问题所在。 - ANeme
@RaulF. 你找出问题的根本原因了吗? - Charles C.
遇到了同样的问题,尝试解决但没有成功,无法理解问题所在,同样的应用在安卓上运行良好,我的安卓开发者朋友简单地使用了像这里我们使用的动画相机。 - Sohaib Siddique

0
请按照下面的代码进行操作。
if self.allSelectedActiveMarkers.count > 0 {
               let firstLocation = CLLocationCoordinate2D(latitude: self.allSelectedActiveMarkers.first?.currentLat ?? 0.0, longitude: self.allSelectedActiveMarkers.first?.currentLong ?? 0.0)
                 var bounds = GMSCoordinateBounds(coordinate: firstLocation, coordinate: firstLocation)
                 for marker in self.allSelectedActiveMarkers {
                   bounds = bounds.includingCoordinate(marker.position)
                 }
                let update = GMSCameraUpdate.fit(bounds, withPadding: CGFloat(20))
                self.mapView.animate(with: update)
             }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接