LoginSignup
eri1208
@eri1208

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

swiftUI、Mapkitを使った地図の中心位置情報を取得したい

解決したいこと

iOS17.0~のswiftUI、Mapkitを使った地図の中心位置情報を取得したいです。
下図のように、MapReaderでMapを囲みタップした位置を取得するのではなく、

MapReader { reader in
                    Map()
                        .onTapGesture(perform: { screenLocation in
                            guard let location = reader.convert(screenLocation, from: .local) else { return }
                            print("tapped point: ", location)
                        })
                }

タップせず、地図に表示されている中心値の緯度経度が知りたいです。
(現在地ではなく、地図を移動させた時も、その中心の緯度経度が知りたい。タップせず。)

自分で試したこと

@State private var position: MapCameraPosition = .userLocation(fallback: .automatic)

var location = position.camera?.centerCoodinate

上記のようにやって、locationにセンターの位置情報が入るかなーと思いきや現在位置しか入らなかった・・・。

困ってます。どなたか回答お願いいたします。

0

1Answer

↓これでどうでしょうか?

        Map(position: $position, interactionModes: .all)
            .onMapCameraChange(frequency: .onEnd, { mapCameraUpdateContext in
                print("now center: ", mapCameraUpdateContext.region.center)
            })
1Like

Comments

  1. @eri1208

    Questioner

    できました!本当に助かりました。
    ありがとうございます!

Your answer might help someone💌