我在 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 位置。提前致谢!
Best Answer-推荐答案 strong>
您不能通过使用纬度和经度直接将 CLLocationCoordinate2D 转换为 CGPoint 。大多数 map API 将提供一种方法来为您执行此操作。
Google 的 Map API 提供 pointFromCoordinate它在 GMSMapView 的框架中创建一个 CGPoint 。这是 GMSProjection 上的一个方法,您可以从 GMSMapView 上的 'projection' 属性获得。
然后您可以使用 Apple 的 convertPoint:toView: 将其转换为另一个 UIView 的坐标空间。
例如:
let location = mapView.userLocation
let point = mapView.projection.pointFromCoordinate(location.coordinate)
let pointInNewView = newView.convert(point, from: mapView)
关于ios - 将 CLLocationCoordinate2D 坐标转换为 CGPoint Swift,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/45988381/
|