Skip to content

Instantly share code, notes, and snippets.

@jeckymodi
Created October 30, 2019 10:23
Show Gist options
  • Save jeckymodi/86f4a41cbfcf95ea60d541b99286e86b to your computer and use it in GitHub Desktop.
Save jeckymodi/86f4a41cbfcf95ea60d541b99286e86b to your computer and use it in GitHub Desktop.
Google map circle In Out animation
//
// ViewController.swift
// GoogleMapPinDropAnimation
//
// Created by Jecky Modi on 27/10/19.
// Copyright © 2019 Jecky Modi. All rights reserved.
//
import UIKit
import GoogleMaps
enum CircleAnimation{
case In
case Out
}
class ViewController: UIViewController {
@IBOutlet weak var mapView: GMSMapView!
var circleAnimation = CircleAnimation.Out
var arrRadius : [GMSCircle] = [GMSCircle]()
var radius : Int = 0
var timer : Timer = Timer()
let location = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
mapView.camera = camera
let marker = GMSMarker()
marker.position = location
marker.title = "Sydney"
marker.icon = UIImage(named: "pin")
marker.snippet = "Australia"
marker.map = mapView
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.timer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(self.timerFired), userInfo: nil, repeats: true)
}
let circleCenter = location//change to your center point
let circ = GMSCircle(position: circleCenter, radius: 5000)//radius in meters
circ.fillColor = UIColor(red: 0.35, green: 0, blue: 0, alpha: 0.05)
circ.strokeColor = UIColor.red
circ.strokeWidth = 1
let update = GMSCameraUpdate.fit(circ.bounds())
mapView.animate(with: update)
}
@objc func timerFired(){
let circleCenter = location//change to your center point
let circ = GMSCircle(position: circleCenter, radius: CLLocationDistance(radius))//radius in meters
circ.fillColor = UIColor(red: 0.35, green: 0, blue: 0, alpha: 0.05)
circ.strokeColor = UIColor.red
circ.strokeWidth = 1
if circleAnimation == .Out{
radius += 50
if radius >= 5000{
radius = 5000
circleAnimation = .In
}
}else{
radius -= 50
if radius <= 0{
radius = 0
circleAnimation = .Out
}
}
arrRadius.append(circ)
circ.map = mapView
if arrRadius.count > 0{
for i in 0..<arrRadius.count - 1{
let circle = arrRadius[i]
circle.map = nil
arrRadius.removeAll(where: { $0 == circle})
}
}
}
}
extension GMSCircle {
func bounds () -> GMSCoordinateBounds {
func locationMinMax(_ positive : Bool) -> CLLocationCoordinate2D {
let sign: Double = positive ? 1 : -1
let dx = sign * self.radius / 6378000 * (180 / .pi)
let lat = position.latitude + dx
let lon = position.longitude + dx / cos(position.latitude * .pi / 180)
return CLLocationCoordinate2D(latitude: lat, longitude: lon)
}
return GMSCoordinateBounds(coordinate: locationMinMax(true),
coordinate: locationMinMax(false))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment