Created
May 31, 2017 14:53
-
-
Save Savchukv/4c36e1f4e5058e6000bae3b605d95624 to your computer and use it in GitHub Desktop.
Example Google map ViewController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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