|
| 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 | + let newYork = CLLocationCoordinate2D(latitude: 40.7230260, longitude: -73.9988390) |
| 21 | + |
| 22 | + var body: some View { |
| 23 | + PanoramaView(viewModel: viewModel) |
| 24 | + .task { |
| 25 | + viewModel.move(near: newYork) |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// MARK: - ViewModel |
| 31 | + |
| 32 | +class PanoramaViewModel: NSObject, ObservableObject { |
| 33 | + |
| 34 | + let panoramaView: GMSPanoramaView = { |
| 35 | + let panoramaView = GMSPanoramaView() |
| 36 | + return panoramaView |
| 37 | + }() |
| 38 | + |
| 39 | + override init() { |
| 40 | + super.init() |
| 41 | + panoramaView.delegate = self |
| 42 | + } |
| 43 | + |
| 44 | + func move(near coordinate: CLLocationCoordinate2D) { |
| 45 | + panoramaView.moveNearCoordinate(coordinate) |
| 46 | + } |
| 47 | + |
| 48 | + func move(to panoramaID: String) { |
| 49 | + panoramaView.move(toPanoramaID: panoramaID) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +extension PanoramaViewModel: GMSPanoramaViewDelegate { |
| 54 | + func panoramaView(_ panoramaView: GMSPanoramaView, didMoveTo panorama: GMSPanorama?) { |
| 55 | + let panoramaID = panorama?.panoramaID ?? "Unknown" |
| 56 | + print("Did move to panorama: \(panoramaID)") |
| 57 | + } |
| 58 | + |
| 59 | + func panoramaView(_ view: GMSPanoramaView, didMoveTo panorama: GMSPanorama, nearCoordinate coordinate: CLLocationCoordinate2D) { |
| 60 | + print("Did move to panorama: \(panorama.panoramaID) near coordinate: \(coordinate)") |
| 61 | + } |
| 62 | + |
| 63 | + func panoramaViewDidStartRendering(_ panoramaView: GMSPanoramaView) { |
| 64 | + print("Panorama view started rendering") |
| 65 | + } |
| 66 | + |
| 67 | + func panoramaViewDidFinishRendering(_ panoramaView: GMSPanoramaView) { |
| 68 | + print("Panorama did finish rendering") |
| 69 | + } |
| 70 | + |
| 71 | + func panoramaView(_ view: GMSPanoramaView, error: any Error, onMoveNearCoordinate coordinate: CLLocationCoordinate2D) { |
| 72 | + print("Failed to move to coordinate: \(coordinate)") |
| 73 | + } |
| 74 | + |
| 75 | + func panoramaView(_ view: GMSPanoramaView, error: any Error, onMoveToPanoramaID panoramaID: String) { |
| 76 | + print("Failed to move to panoramaID: \(panoramaID)") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// MARK: - UIViewRepresentable |
| 81 | + |
| 82 | +struct PanoramaView: UIViewRepresentable { |
| 83 | + let viewModel: PanoramaViewModel |
| 84 | + |
| 85 | + init(viewModel: PanoramaViewModel) { |
| 86 | + self.viewModel = viewModel |
| 87 | + } |
| 88 | + |
| 89 | + func makeUIView(context: Context) -> GMSPanoramaView { |
| 90 | + return viewModel.panoramaView |
| 91 | + } |
| 92 | + |
| 93 | + func updateUIView(_ uiView: GMSPanoramaView, context: Context) {} |
| 94 | +} |
| 95 | + |
| 96 | + |
0 commit comments