2017-08-31 97 views
1

我在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位置。提前致謝!

+0

的可能的複製[如何獲得與谷歌地圖SDK適用於iOS的位置CGPoint?](HTTPS ://stackoverflow.com/questions/15066567/how-to-get-cgpoint-of-location-with-google-maps-sdk-for-ios) –

+0

檢查此答案:https://stackoverflow.com/a/ 15013705/3824808 –

回答

2

通過使用緯度和經度,您不能直接將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

解決它!萬分感謝!!! –