Google Maps Integration iOS Swift. Part 2

Andrii Petrovskyi
3 min readJul 8, 2021

Description: — GoogleMaps with custom theme and Clustering. iOS Swift

Next Step! We need to Implement CLLocationManager to ask user location usage, and to get his current location:

  1. I decide to create some object which will be responsive for this task, and called him LocationManager. But before we need to create two protocols to manage all needed operations, firsts for input actions, second-one should be a delegate to give us some callbacks (like didUpdateLocations.. etc). So our Protocol should be something like this:
import CoreLocationimport Foundationprotocol LocationServiceDelegate: AnyObject {  func updateLocation(_ location: CLLocationCoordinate2D?)  func authorizattionStatusDidChanged(isAccessGranted: Bool)}protocol LocationServiceProtocol: CLLocationManagerDelegate {  func setDelegate(_ delegate: LocationServiceDelegate?)  func startLocationService()  func stopLocationService()}

LocationServiceDelegate. — contains two methods one for react to Location update. — updateLocation, and one to react on authorizationStatus changed
Actually if you need to implement some reaction on this events, also delegate conformed to AnyObject to let us give weak modifier for objects of LocationServiceDelegate type

LocationServiceProtocol- contains three methods and confirmed to CLLocationManagerDelegate:

setDelegate — it’s delegate setter

startLocationsService — will notify our service the he can start updatingLocations, requestAuthorization, etc…

stopLocationService — will notify out service that we need to stop updatingLocation, deist locationManager, etc…

So our Location Service will be look loke this:

final class LocationService: NSObject, LocationServiceProtocol {  private var locationManager: CLLocationManager?  private weak var delegate: LocationServiceDelegate? func setDelegate(_ delegate: LocationServiceDelegate?) {  self.delegate = delegate } override init() {  self.locationManager = CLLocationManager()  super.init() }}extension LocationService: CLLocationManagerDelegate {// This method will notify our delegate if location did updated func locationManager(_ manager: CLLocationManager,       didUpdateLocations locations: [CLLocation]) {  delegate?.updateLocation(locations.last?.coordinate) }// This method will notify our delegate if Authorization status did changed func locationManager(_ manager: CLLocationManager,  didChangeAuthorization status: CLAuthorizationStatus) {  switch status {   case .authorizedAlways, .authorizedWhenInUse:        delegate?.authorizattionStatusDidChanged(isAccessGranted: true)  default:   delegate?.authorizattionStatusDidChanged(isAccessGranted: false)}}func startLocationService() {   if locationManger == nil {
loactionManager = CLLocationManager()
}
locationManager?.delegate = self locationManager?.desiredAccuracy = kCLLocationAccuracyBest locationManager?.requestAlwaysAuthorization() locationManager?.startUpdatingLocation() locationManager?.distanceFilter = CLLocationDistanceMax} func stopLocationService() { locationManager?.stopUpdatingLocation() locationManager?.delegate = nil locationManager = nil }}

Great, now lets create instance of Location Service for this time inside of MapViewController.swift, and then we can startLocationService() on viewDidLoad and stopLocationService, let’s say on viewWillDissapear:

import UIKitimport GoogleMapsstruct KyivLocation {  static let latitude: CLLocationDegrees = 50.450001  static let longitude: CLLocationDegrees = 30.523333}final class MapViewController: UIViewController {  private lazy var mapView: GMSMapView? = {          return GMSMapView(frame: view.bounds,                            camera: camera)  }()   private lazy var camera: GMSCameraPosition = {         return GMSCameraPosition(latitude: KyivLocation.latitude,                                  longitude: KyivLocation.longitude,                                  zoom: 7)}()
private let locationService: LocationServiceProtocol = LocationService()
override func viewDidLoad() {
super.viewDidLoad()
layoutMapView()
locationService.startLocationService()
} override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) locationService.stopLocationService() }}

Nice, let’s run the app, and we should see next screen:

Cool! We done with it

Link to 1 part

Link to 3 part

Lint to 4 part

Link to 5 part

--

--