Skip to content

Commit ca0384d

Browse files
committed
Add small example
1 parent 687665e commit ca0384d

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

GoogleMaps-SwiftUI/GoogleMaps-SwiftUI/Dialog/Helper/MapExamplesViewModel.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import GoogleMaps
1717
class MapExamplesViewModel: ObservableObject {
1818

1919
@Published var examples: [MapExample] = [
20+
MapExample(
21+
title: "Basic panorama",
22+
description: "A simple panorama. Shows how to initalize and present a panorama.",
23+
destination: AnyView(BasicPanorama())
24+
),
2025
MapExample(
2126
title: "Basic map",
2227
description: "A simple map. Shows how to initalize and update map options.",
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2024 Google LLC. All rights reserved.
2+
//
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
5+
// file except in compliance with the License. You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software distributed under
10+
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
// ANY KIND, either express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
import SwiftUI
15+
import GoogleMaps
16+
17+
struct BasicPanorama: View {
18+
19+
let viewModel = PanoramaViewModel()
20+
21+
var body: some View {
22+
PanoramaView(viewModel: viewModel)
23+
.task {
24+
viewModel.move(near: .newYork)
25+
}
26+
}
27+
}
28+
29+
// MARK: - ViewModel
30+
31+
class PanoramaViewModel: NSObject, ObservableObject {
32+
33+
let panoramaView: GMSPanoramaView = {
34+
let panoramaView = GMSPanoramaView()
35+
return panoramaView
36+
}()
37+
38+
override init() {
39+
super.init()
40+
panoramaView.delegate = self
41+
}
42+
43+
func move(near coordinate: CLLocationCoordinate2D) {
44+
panoramaView.moveNearCoordinate(coordinate)
45+
}
46+
47+
func move(to panoramaID: String) {
48+
panoramaView.move(toPanoramaID: panoramaID)
49+
}
50+
}
51+
52+
extension PanoramaViewModel: GMSPanoramaViewDelegate {
53+
func panoramaView(_ panoramaView: GMSPanoramaView, didMoveTo panorama: GMSPanorama?) {
54+
let panoramaID = panorama?.panoramaID ?? "Unknown"
55+
print("Did move to panorama: \(panoramaID)")
56+
}
57+
58+
func panoramaView(_ view: GMSPanoramaView, didMoveTo panorama: GMSPanorama, nearCoordinate coordinate: CLLocationCoordinate2D) {
59+
print("Did move to panorama: \(panorama.panoramaID) near coordinate: \(coordinate)")
60+
}
61+
62+
func panoramaViewDidStartRendering(_ panoramaView: GMSPanoramaView) {
63+
print("Panorama view started rendering")
64+
}
65+
66+
func panoramaViewDidFinishRendering(_ panoramaView: GMSPanoramaView) {
67+
print("Panorama did finish rendering")
68+
}
69+
70+
func panoramaView(_ view: GMSPanoramaView, error: any Error, onMoveNearCoordinate coordinate: CLLocationCoordinate2D) {
71+
print("Failed to move to coordinate: \(coordinate)")
72+
}
73+
74+
func panoramaView(_ view: GMSPanoramaView, error: any Error, onMoveToPanoramaID panoramaID: String) {
75+
print("Failed to move to panoramaID: \(panoramaID)")
76+
}
77+
}
78+
79+
// MARK: - UIViewRepresentable
80+
81+
struct PanoramaView: UIViewRepresentable {
82+
let viewModel: PanoramaViewModel
83+
84+
init(viewModel: PanoramaViewModel) {
85+
self.viewModel = viewModel
86+
}
87+
88+
func makeUIView(context: Context) -> GMSPanoramaView {
89+
return viewModel.panoramaView
90+
}
91+
92+
func updateUIView(_ uiView: GMSPanoramaView, context: Context) {}
93+
}
94+
95+

0 commit comments

Comments
 (0)