/
MarkerInfoWindowViewController.swift
executable file
·87 lines (72 loc) · 2.85 KB
/
MarkerInfoWindowViewController.swift
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
// Copyright 2020 Google LLC. All rights reserved.
//
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, either express or implied. See the License for the specific language governing
// permissions and limitations under the License.
import GoogleMaps
import UIKit
final class MarkerInfoWindowViewController: UIViewController {
private let sydneyMarker = GMSMarker(
position: CLLocationCoordinate2D(latitude: -33.8683, longitude: 151.2086))
private let melbourneMarker = GMSMarker(
position: CLLocationCoordinate2D(latitude: -37.81969, longitude: 144.966085))
private let brisbaneMarker = GMSMarker(
position: CLLocationCoordinate2D(latitude: -27.4710107, longitude: 153.0234489))
private lazy var contentView: UIImageView = {
return UIImageView(image: UIImage(named: "aeroplane"))
}()
override func loadView() {
let cameraPosition = GMSCameraPosition(latitude: -37.81969, longitude: 144.966085, zoom: 4)
let mapView = GMSMapView(frame: .zero, camera: cameraPosition)
mapView.delegate = self
view = mapView
sydneyMarker.title = "Sydney"
sydneyMarker.snippet = "Population: 4,605,992"
sydneyMarker.map = mapView
melbourneMarker.title = "Melbourne"
melbourneMarker.snippet = "Population: 4,169,103"
melbourneMarker.map = mapView
brisbaneMarker.title = "Brisbane"
brisbaneMarker.snippet = "Population: 2,189,878"
brisbaneMarker.map = mapView
}
}
extension MarkerInfoWindowViewController: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
if marker == sydneyMarker {
return contentView
}
return nil
}
func mapView(_ mapView: GMSMapView, markerInfoContents marker: GMSMarker) -> UIView? {
if marker == brisbaneMarker {
return contentView
}
return nil
}
func mapView(_ mapView: GMSMapView, didCloseInfoWindowOf marker: GMSMarker) {
showToast(message: "Info window for marker \(marker.title ?? "") closed.")
}
func mapView(_ mapView: GMSMapView, didLongPressInfoWindowOf marker: GMSMarker) {
showToast(message: "Info window for marker \(marker.title ?? "") long pressed.")
}
}
extension UIViewController {
func showToast(message: String) {
let toast = UIAlertController(title: nil, message: message, preferredStyle: .alert)
present(
toast, animated: true,
completion: {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(2)) {
toast.dismiss(animated: true)
}
})
}
}