forked from truongminhthang/CodeSnippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2F071362-B07E-42A7-B697-6D5C52A55E4F.codesnippet
97 lines (89 loc) · 3.46 KB
/
2F071362-B07E-42A7-B697-6D5C52A55E4F.codesnippet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDECodeSnippetCompletionScopes</key>
<array>
<string>TopLevel</string>
</array>
<key>IDECodeSnippetContents</key>
<string>
@IBOutlet weak var mapView: GMSMapView!
var currentLocation: CLLocation?
var placesClient = GMSPlacesClient.shared()
var zoomLevel: Float = 15.0
var mapbounds:GMSCoordinateBounds? {
if let visibleRegion = mapView?.projection.visibleRegion() {
return GMSCoordinateBounds(coordinate: visibleRegion.farLeft, coordinate: visibleRegion.nearRight)
}
return nil
}
lazy var locationManager: CLLocationManager = {
let locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.distanceFilter = 50
locationManager.startUpdatingLocation()
locationManager.delegate = self
return locationManager
}()
extension MapVC: CLLocationManagerDelegate {
func setupLocationManager() {
let _ = locationManager
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
mapView.padding = UIEdgeInsets(top: 16, left: 0, bottom: 150, right: 0)
mapView.delegate = self
// Add the map to the view, hide it until we've got a location update.
mapView.isHidden = true
}
// Handle incoming location events.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let currentLocation = locations.last else { return }
self.currentLocation = currentLocation
print("Current Location: \(currentLocation)")
let camera = GMSCameraPosition.camera(withLatitude: currentLocation.coordinate.latitude,
longitude: currentLocation.coordinate.longitude,
zoom: zoomLevel)
if mapView.isHidden {
mapView.isHidden = false
mapView.camera = camera
} else {
mapView.animate(to: camera)
}
}
// Handle authorization for the location manager.
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .restricted:
print("Location access was restricted.")
case .denied:
print("User denied access to location.")
// Display the map using the default location.
mapView.isHidden = false
case .notDetermined:
print("Location status not determined.")
case .authorizedAlways: fallthrough
case .authorizedWhenInUse:
print("Location status is OK.")
}
}
// Handle location manager errors.
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
locationManager.stopUpdatingLocation()
print("Error: \(error)")
}
}
</string>
<key>IDECodeSnippetIdentifier</key>
<string>2F071362-B07E-42A7-B697-6D5C52A55E4F</string>
<key>IDECodeSnippetLanguage</key>
<string>Xcode.SourceCodeLanguage.Swift</string>
<key>IDECodeSnippetTitle</key>
<string>Swift 3: setup location Manager and cowork with google map</string>
<key>IDECodeSnippetUserSnippet</key>
<true/>
<key>IDECodeSnippetVersion</key>
<integer>2</integer>
</dict>
</plist>