Skip to content

Instantly share code, notes, and snippets.

@Savchukv
Created May 31, 2017 14:53
Show Gist options
  • Save Savchukv/4c36e1f4e5058e6000bae3b605d95624 to your computer and use it in GitHub Desktop.
Save Savchukv/4c36e1f4e5058e6000bae3b605d95624 to your computer and use it in GitHub Desktop.
Example Google map ViewController
//
// GoogleMapViewController.swift
//
// Created by Vasiliy Savchuk on 11/21/16.
// Copyright © 2016 All rights reserved.
//
import UIKit
import GoogleMaps
class GoogleMapViewController: UIViewController {
var mapView : GMSMapView?
var locationManager = CLLocationManager()
//MARK - Lifecicle Object
override func viewDidLoad() {
super.viewDidLoad()
self.createMap()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
//MARK: Methods
func createMap() {
self.mapView = GMSMapView.init(frame: CGRect.zero)
self.view = self.mapView
self.mapView?.settings.myLocationButton = true
}
}
//MARK: CLLocationManagerDelegate
extension GoogleMapViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView?.isMyLocationEnabled = true
mapView?.settings.myLocationButton = true
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView?.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment