我在iOS Swift应用中有一个谷歌地图。我正在尝试获取当前用户坐标的CGPoint位置,以便我可以应用一些动画。但是我无法在我的坐标中获得CGPoint的位置。
我基本上是在尝试为当前用户标记添加脉冲动画。这是我的动画代码 -
class Pulsing: CALayer {
var animationGroup = CAAnimationGroup()
var initialPulseSacle:Float = 0
var nextPluseAfter:TimeInterval = 0
var animationDuration:TimeInterval = 1.5
var radius:CGFloat = 200
var numberOfPulse:Float = Float.infinity
override init(layer: Any) {
super.init(layer: layer)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init(numberOfPulse:Float = Float.infinity, radius:CGFloat, position:CGPoint){
super.init()
self.backgroundColor = UIColor.blue.cgColor
self.contentsScale = UIScreen.main.scale
self.opacity = 0
self.radius = radius
self.position = position
self.bounds = CGRect(x: 0, y: 0, width: radius*2, height: radius*2)
self.cornerRadius = radius
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
self.setupAnimationGroup()
DispatchQueue.main.async {
self.add(self.animationGroup, forKey: "pulse")
}
}
}
func createScaleAnimation () -> CABasicAnimation{
let scaleAnimation = CABasicAnimation(keyPath: "transferm.scale.xy")
scaleAnimation.fromValue = NSNumber(value: initialPulseSacle)
scaleAnimation.toValue = NSNumber(value: 1)
scaleAnimation.duration = animationDuration
return scaleAnimation
}
func createOpacityAnimation () -> CAKeyframeAnimation{
let opacityAnimation = CAKeyframeAnimation(keyPath: "opacity")
opacityAnimation.duration = animationDuration
opacityAnimation.values = [4.0, 8.0, 0]
opacityAnimation.keyTimes = [0, 0.2, 1]
return opacityAnimation
}
func setupAnimationGroup(){
self.animationGroup = CAAnimationGroup()
self.animationGroup.duration = animationDuration + nextPluseAfter
self.animationGroup.repeatCount = numberOfPulse
let defaultCurve = CAMediaTimingFunction(name: kCAMediaTimingFunctionDefault)
self.animationGroup.timingFunction = defaultCurve
self.animationGroup.animations = [createScaleAnimation(),createOpacityAnimation()]
}
}
然后这就是我在设置标记方法中调用它的方法 -
func setuplocationMarker(coordinate: CLLocationCoordinate2D,address:String) {
locationMarker = GMSMarker(position: coordinate)
locationMarker.map = viewMap
locationMarker.title = address
locationMarker.appearAnimation = GMSMarkerAnimation.pop
locationMarker.icon = GMSMarker.markerImage(with: UIColor.red)
locationMarker.opacity = 0.75
let pulse = Pulsing(numberOfPulse: 1, radius: 80, position:CGPoint(x: CGFloat(Float(coordinate.latitude)), y: CGFloat(Float(coordinate.longitude))))
pulse.animationDuration = 0.8
pulse.backgroundColor = UIColor.blue.cgColor
self.view.layer.insertSublayer(pulse, below: locationMarker.layer)
}
使用此代码,它会在顶部角落显示脉冲动画,而不是位置标记周围。如何为我的标记获得正确的cgpoint位置。在此先感谢!
答案 0 :(得分:2)
您无法使用纬度和经度直接将CLLocationCoordinate2D
转换为CGPoint
。大多数地图API将为您提供一种方法。
Google的Map API提供pointFromCoordinate,可在CGPoint
的框架中创建GMSMapView
。这是GMSProjection
上的一种方法,您可以从GMSMapView
上的“投影”属性获取该方法。
然后,您可以使用Apple的convertPoint:toView:将其转换为另一个UIView
的坐标空间
例如:
let location = mapView.userLocation
let point = mapView.projection.pointFromCoordinate(location.coordinate)
let pointInNewView = newView.convert(point, from: mapView)