【问题标题】:iOS Swift Google Maps SDK showing markers at specific zoom level?iOS Swift Google Maps SDK 在特定缩放级别显示标记?
【发布时间】:2018-07-04 19:54:49
【问题描述】:

我的谷歌地图上有标记,一旦用户缩放到地图上的特定缩放级别,我想将它们设置为不隐藏,然后当用户缩小时,标记再次隐藏。

在 Snapchat 的 SnapMap 中可以看到类似的功能。

如何实现这种行为?

在谷歌地图的didChangeposition 委托方法中,我可以掌握当前的缩放级别,但我如何才能在标记中设置动画?我没有看到访问当前显示的标记数组的方法。

有什么想法吗?

【问题讨论】:

    标签: ios swift google-maps


    【解决方案1】:

    您可以使用 googlemaps 中的“didchange”委托 举个例子:

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

    如果你想添加动画,这对我有用

    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()
    }
    

    【讨论】:

    • 继续重新制作/清除标记是否比隐藏/取消隐藏更密集?
    • 如果确实需要删除所有标记,那么我认为最好使用 mapview.clear (),因为它更安全,而且为了性能,我在我的应用程序中没有发现任何问题
    【解决方案2】:

    这就是我在 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 是包含我的注释的 Array 类型的属性。 Google SDK 中可能有类似的东西。看这个页面:https://developers.google.com/maps/documentation/ios-sdk/events。也许mapView:didChangeCameraPosition:是吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-30
      • 2014-01-15
      • 2022-06-16
      • 2013-12-01
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多