ios - 将 CLLocationCoordinate2D 坐标转换为 CGPoint Swift
<p><p>我在 iOS Swift 应用中有一个谷歌地图。我正在尝试获取当前用户坐标的 CGPoint 位置,以便我可以应用一些动画。但我无法在 CGPoint 中获得我的坐标位置。 </p>
<p>我基本上是在尝试为我当前的用户标记添加脉冲动画。这是我的动画代码 - </p>
<pre><code>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 =
opacityAnimation.keyTimes =
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 =
}
}
</code></pre>
<p>然后这就是我在设置标记方法中的调用方式 - </p>
<pre><code>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)
}
</code></pre>
<p>使用此代码,它会在顶部让角处显示脉冲动画,而不是在位置标记周围。我如何为我的标记获得正确的 cgpoint 位置。提前致谢!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您不能通过使用纬度和经度直接将 <code>CLLocationCoordinate2D</code> 转换为 <code>CGPoint</code>。大多数 mapAPI 将提供一种方法来为您执行此操作。 </p>
<p>Google 的 Map API 提供 <a href="https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_projection.html#a8822e27ca2557b7f2f484781f956ff5c" rel="noreferrer noopener nofollow">pointFromCoordinate</a>它在 <code>GMSMapView</code> 的框架中创建一个 <code>CGPoint</code>。这是 <code>GMSProjection</code> 上的一个方法,您可以从 <code>GMSMapView</code> 上的 'projection' 属性获得。 </p>
<p>然后您可以使用 Apple 的 <a href="https://developer.apple.com/documentation/mapkit/mkmapview/1452694-convert" rel="noreferrer noopener nofollow">convertPoint:toView:</a> 将其转换为另一个 <code>UIView</code> 的坐标空间。
例如:</p>
<pre><code>let location = mapView.userLocation
let point = mapView.projection.pointFromCoordinate(location.coordinate)
let pointInNewView = newView.convert(point, from: mapView)
</code></pre></p>
<p style="font-size: 20px;">关于ios - 将 CLLocationCoordinate2D 坐标转换为 CGPoint Swift,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/45988381/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/45988381/
</a>
</p>
页:
[1]