Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps integration into SwiftUI

Im fairly new into programming with Xcode and SwiftUI and am having trouble integrating Google Maps into a SwiftUI project.

I added all the right API keys into my AppDelegate.swift file and have created a view called GoogMapView that i am trying to use to display an instance of Google maps. This is my code in the file GoogMapView:

import SwiftUI
import MapKit
import UIKit
import GoogleMaps
import GooglePlaces

struct GoogMapView : UIViewRepresentable {
    func makeUIView(context: Context) -> GMSMapView {
        GMSMapView(frame: .zero)
    }

    func updateUIView(_ view: GMSMapView, context: Context) {
        let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        view = mapView

        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView
    }

I keep getting an error at 'view = mapView' but everything i tried has failed. Any idea how to set this up so i can call it in a main view?

like image 420
zlyt Avatar asked Oct 02 '19 01:10

zlyt


People also ask

How do I display a map in SwiftUI?

To display a map with the region of your choosing, follow these steps: import MapKit. Add a @State property to define a variable named region as a MKCoordinateRegion that specifies the center coordinate and zoom level for the map. Add a Map element to the body of the MapView and use the region as a parameter.

How do I open Google Maps in Swift?

Create the function. Create a new Swift file from Xcode and call it OpenMapDirections, then copy/paste the following code in it: We will use the above code to present an alert controller with 2 actions: one for opening Google Maps, the other to open Apple Maps. Here we pass the coordinate for both directly.


1 Answers

I am also a new iOS developer. I was looking into the same thing and stumbled on your question. Since I am new to iOS, I can't claim it follows all the proper conventions, but this code works to display the map on the simulator with SwiftUI at a basic level. Solution is based on your code, and Matteo's observation.

import SwiftUI
import UIKit
import GoogleMaps

struct ContentView: 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
    }

}
like image 166
vikingmobile Avatar answered Nov 11 '22 06:11

vikingmobile