简体   繁体   English

iOS Swift Google Maps SDK在特定缩放级别显示标记吗?

[英]iOS Swift Google Maps SDK showing markers at specific zoom level?

I have markers on my google map and I want to animate them to become un-hidden once the user zooms to a specific zoom level on the map, and then when the user zooms back out, the marker is hidden again. 我在Google地图上有标记,并且一旦用户缩放到地图上的特定缩放级别,我想为其设置动画以使其不隐藏,然后当用户缩小时,标记再次隐藏。

A similar functionality can be seen in Snapchat's SnapMap. 在Snapchat的SnapMap中可以看到类似的功能。

How is this behavior attainable? 如何获得这种行为?

In google map's didChangeposition delegate method I can get a hold of the current zoom level, but from there how could I animate in and out the markers? 在谷歌地图的didChangeposition委托方法中,我可以保留当前的缩放级别,但是从那里我可以如何对标记进行动画处理? I'm not seeing a way to access the array of markers currently being shown. 我没有看到访问当前显示的标记数组的方法。

Any ideas? 有任何想法吗?

This is how I did it using the standard MapKit in Swift: 这是我在Swift中使用标准MapKit进行的操作:

/**
     Tells the mapview delegate the mapview visible region was changed. The window size is checked then
     the correct icon size is displayed on the map.
     - parameter mapView: The map view whose visible region changed.
     - parameter animated: If true, the change to the new region was animated.
     */
    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {

        let zoomWidth = mapView.visibleMapRect.size.width
        //print(zoomWidth)
        if Int(zoomWidth) < 15000  {
            self.mapView.addAnnotations(self.stopArr)
            imageZoom = imageArr[1]! //32px
        } else if Int(zoomWidth) >= 15000 && Int(zoomWidth) < 20000 {
            imageZoom = imageArr[0]! //16px
            self.mapView.addAnnotations(self.stopArr)
        } else {
            imageZoom = imageArr[0]! //16px
            self.mapView.removeAnnotations(self.stopArr)
        }
    }

stopArr is a property of type Array containing my annotations. stopArr是包含我的注释的Array类型的属性。 There is probably something similar in the Google SDK. Google SDK中可能有类似的东西。 Looking at this page: https://developers.google.com/maps/documentation/ios-sdk/events . 查看此页面: https : //developers.google.com/maps/documentation/ios-sdk/events Maybe mapView:didChangeCameraPosition: does it? 也许mapView:didChangeCameraPosition:是吗?

You can use a "didchange" delegate from googlemaps as an example : 您可以使用googlemaps的“ didchange”委托作为示例:

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
    if mapView.camera.zoom >= 16 {
         // ADD YOUR MARKERS HERE
    } else {
        mapView.clear()
    }
}

If you want to add animated in out, this works for me 如果您想添加动画输入,这对我有用

func addMarker() {
        self.markerArray.removeAll()

        for data in yourDataArray {
            let iconView = UIImageView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
            iconView.image = UIImage(named: "YOUR_IMAGE")
            iconView.contentMode = .scaleAspectFit

            // Creates a marker in the center of the map.
            let marker = GMSMarker()
            marker.position = CLLocationCoordinate2D(latitude: data.lat, longitude: data.lng)
            marker.iconView = iconView
            marker.tracksViewChanges = true
            marker.map = mapView

            self.markerArray.append(marker)

            UIView.animate(withDuration: 0.7,
                       animations: {
                  marker.iconView?.frame = CGRect(x: 0, y: 0, width: 29.0, height: 34.0)
            }, completion: nil)
        }
}

func removeMarker() {
        for marker in self.markerArray {
            UIView.animate(withDuration: 0.3,
                           animations: {
                   marker.iconView?.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
            }, completion: nil)
        }

        self.mapView.clear()
        self.markerArray.removeAll()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM