애플 맵은 위치 권한을 얻은 후에 userTrackingMode만 조절해주면 내 위치 따라가기가 되는데, 구글맵은 왜 그런게 없는지 모르겠다. 진짜로 안되나 싶어서 찾아봤는데, 되긴되더라.
addObserver, observeValueForKeyPath 를 이용하는 거였다.
출처: https://stackoverflow.com/a/38627199/1025379
struct GoogleMapView: UIViewRepresentable { @State var coordinator = Coordinator() func makeUIView(context _: Context) -> GMSMapView { let view = GMSMapView(frame: .zero) view.isMyLocationEnabled = true view.animate(toZoom: 18) view.addObserver(coordinator, forKeyPath: "myLocation", options: .new, context: nil) } func updateUIView(_ uiView: GMSMapView, context _: UIViewRepresentableContext<GoogleMapView>) {} func makeCoordinator() -> GoogleMapView.Coordinator { return coordinator } static func dismantleUIView(_ uiView: GMSMapView, coordinator: GoogleMapView.Coordinator) { uiView.removeObserver(coordinator, forKeyPath: "myLocation") } final class Coordinator: NSObject { override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) { if let location = change?[.newKey] as? CLLocation, let mapView = object as? GMSMapView { mapView.animate(toLocation: location.coordinate) } } } }
만약 따라가기 조건도 넣고 싶다면 mapView의 tag를 적절히 이용하면 된다.
구글은 왜 이런 기능을 안 넣어둔걸까…