SwiftUI-Display the map using MapKit

Keep a reminder of how to view the map in SWift UI.

Displaying a map in Swift UI was very easy. Let's display it like the image below.

スクリーンショット 2020-11-06 15.45.48.png

Code implementation

import SwiftUI
import MapKit

struct ContentView: View {
    @State private var location = MKCoordinateRegion(center: .init(latitude: 35.677735, longitude: 139.764740), latitudinalMeters: 500, longitudinalMeters: 500) //Coordinates to be displayed, range setting, display centered on the set coordinates

    var body: some View {
        Map(coordinateRegion: self.$location)
    }
}

With the above code alone, the safe area creates spaces above and below the entire screen instead of the entire screen. So, by setting the code below, you can ignore the safe area and display the map on the entire screen.

.edgesIgnoringSafeArea(.all)

Final source code

import SwiftUI
import MapKit

struct ContentView: View {
    @State private var location = MKCoordinateRegion(center: .init(latitude: 35.677735, longitude: 139.764740), latitudinalMeters: 500, longitudinalMeters: 500) //Coordinates to be displayed, range setting, display centered on the set coordinates

    var body: some View {
        Map(coordinateRegion: self.$location)
            .edgesIgnoringSafeArea(.all) //By setting this, the map will be displayed on the entire screen

    }
}

Recommended Posts

SwiftUI-Display the map using MapKit
How to get the contents of Map using for statement Memorandum
User evaluation using the like function
[Parse] Hit the API using callFunctionInBackground
Try using the messaging system Pulsar