gpt4 book ai didi

ios - 将 CLLocationCoordinate2D 坐标转换为 CGPoint Swift

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:22:41 25 4
gpt4 key购买 nike

我在 iOS Swift 应用程序中有一个 Google map 。我正在尝试获取当前用户坐标的 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 位置。提前致谢!

最佳答案

您不能使用纬度和经度将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com