Skip to content

Instantly share code, notes, and snippets.

@kwmt
Created October 5, 2019 17:13
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwmt/1df17577c59e33da1c4c2eb982f21bbf to your computer and use it in GitHub Desktop.
Save kwmt/1df17577c59e33da1c4c2eb982f21bbf to your computer and use it in GitHub Desktop.
how to googlemaps view on swiftui
import UIKit
import GoogleMaps
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// https://developers.google.com/maps/documentation/ios-sdk/get-api-key#add_key
GMSServices.provideAPIKey("YOUR_API_KEY")
return true
}
}
import SwiftUI
struct ContentView: View {
@ObservedObject var viewModel = ContentViewModel()
var body: some View {
NavigationView {
GoogleMapView()
}
}
}
// ref:https://stackoverflow.com/questions/58194015/google-maps-integration-into-swiftui
import SwiftUI
import UIKit
import GoogleMaps
struct GoogleMapView: UIViewRepresentable {
let marker : GMSMarker = GMSMarker()
/// 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: -33.86, longitude: 151.20, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
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: -33.86, longitude: 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
}
}
struct GoogleMapView_Previews: PreviewProvider {
static var previews: some View {
GoogleMapView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment