いつもお世話になっております。
SwiftUIでGoogle Mapを表示させることが出来たのですが、ステッパーで +ーを押した際に、マップのズームレベルを変更するようにしたいですが、うまくいきません。
アドバイスを頂けますでしょうか。
呼び出しもと(一部)
name,latitude,longitudeはJSONファイルから呼び出すようにしております。変数zoomは状態変数@Stateです。
ZStack(alignment: .bottomTrailing){
GoogleMapView(name:self.shop.name,latitude: self.shop.latitude, longitude: self.shop.longitude,zoom: self.$zoom)
.frame(width:geometry.size.width,height: 300)
Stepper(onIncrement: {self.zoom += 5.0}, onDecrement:{self.zoom -= 5.0}){
Text("")
}
}
呼び出し先 GoogleMapView
import SwiftUI
import GoogleMaps
import GooglePlaces
struct GoogleMapView: UIViewRepresentable {
let marker : GMSMarker = GMSMarker()
var name:String
var latitude:Double
var longitude:Double
@Binding var zoom:Float
/// Creates a `UIView` instance to be presented.
func makeUIView(context: Self.Context) -> GMSMapView {
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
let camera = GMSCameraPosition.camera(withLatitude: latitude, longitude: longitude, zoom: 20)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
//ここでズームレベルが変わる予定だった
if self.zoom != 20{
mapView.animate(toZoom: self.zoom)
}
return mapView
}
/// Updates the presented `UIView` (and coordinator) to the latest
/// configuration.
func updateUIView(_ mapView: GMSMapView, context: Self.Context) {
// Creates a marker in the center of the map.
marker.position = CLLocationCoordinate2D(latitude: self.latitude, longitude: self.longitude)
marker.title = self.name
marker.map = mapView
}
}
よろしくお願い致します。
mapView.animate(toZoom: self.zoom)
の行をupdateUIView(_:context:)
側に入れると言うのはやってみましたか?