将CLLocationCoordinate2D坐标转换为CGPoint Swift
问题描述:
我在iOS Swift应用中有Google Map。我正在尝试为我当前的用户坐标获取CGPoint位置,以便我可以应用一些动画。但我无法在CGPoint中获得我的坐标位置。将CLLocationCoordinate2D坐标转换为CGPoint Swift
我基本上试图为我当前的用户标记添加脉冲动画。这里是我的动画代码 -
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
。大多数地图API将为您提供一种方法。
Google's Map API提供pointFromCoordinate,它在GMSMapView
的框架中创建了CGPoint
。这是GMSProjection
上的一种方法,您可以从GMSMapView
上的“投影”属性中获取该方法。
然后,您可以翻译成另一种UIView
坐标空间使用苹果的convertPoint:toView: 例如:
let location = mapView.userLocation
let point = mapView.projection.pointFromCoordinate(location.coordinate)
let pointInNewView = newView.convert(point, from: mapView)
+1
解决它!万分感谢!!! –
的可能的复制[如何获得与谷歌地图SDK适用于iOS的位置CGPoint?](HTTPS ://stackoverflow.com/questions/15066567/how-to-get-cgpoint-of-location-with-google-maps-sdk-for-ios) –
检查此答案:https://stackoverflow.com/a/ 15013705/3824808 –