我正在使用Google map 构建一个项目,并试图在选中它时对 map 标记进行动画处理(看起来像在附加的图像上一样)。

ios - 在Swift iOS中的Google Maps上选择动画 map 标记-LMLPHP
ios - 在Swift iOS中的Google Maps上选择动画 map 标记-LMLPHP

看起来更像是将 map 标记转移到infoWindow中,这有可能吗?我知道iconView上有一个GMSMarker属性,这是可以制作动画的UIView,但是我担心从标记本身调用其他操作可能会出现问题。

我是否应该为标记提供提示窗口,该标记窗口将覆盖标记本身,然后为infoWindow设置动画,还是有其他解决方案?

最佳答案

我使用MapKit做过类似的动画。
从1.13版开始的GoogleMaps SDK上,如果我没有记错的话,您可以采用与MapKit类似的方法。

您有一个名为iconView的属性,并且(几乎)具有与UIView相同的功能。

iconView属性支持UIView 的所有可设置动画的属性的动画,帧和中心除外。

由于此限制,我不得不在iconView内的动画视图上花一些技巧。
另外,要使动画视图的开始居中,必须使iconView的大小为原来的两倍,因此,如果标记过多,则可能是个大问题。

出于好奇,我做了一个示例代码。

在下面的代码中,您可以自定义markerView函数,以在按钮视图上包含按钮,图标和所有您需要的东西。

import UIKit
import GoogleMaps

class MapViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()

        // Create a GMSCameraPosition that tells the map to display the
        // coordinate -33.86,151.20 at zoom level 6.
        let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 14.0)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        mapView.camera = camera
        mapView.delegate = self
        self.view = mapView
        // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.iconView = markerView()
        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView
        // Do any additional setup after loading the view.
    }

    func markerView() -> UIView {
        let backView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 30))

        let markerView = UIView(frame: CGRect(x: 85, y: 0, width: 30, height: 30))
        let labelName = UILabel(frame: CGRect(x: 30, y: 0, width: 70, height: 30))
        markerView.clipsToBounds = true
        labelName.text = "Yyaaaa"
        labelName.textColor = .white
        markerView.addSubview(labelName)
        markerView.backgroundColor = .blue
        markerView.layer.cornerRadius = 15
        markerView.tag = 1
        backView.addSubview(markerView)
        return backView
    }
}

extension MapViewController: GMSMapViewDelegate {
    func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
        let backView = marker.iconView
        if let subViews = backView?.subviews {
            for view in subViews {
                if view.tag == 1 {
                    UIView.animate(withDuration: 0.5, animations: {
                      view.frame = CGRect(x: 85, y: 0, width: 115, height: 30)
                    })
                }
            }
        }
        return nil
    }
}

10-08 07:25
查看更多