谷歌地图集成到SwiftUI中



我对Xcode和SwiftUI编程相当陌生,并且在将Google Maps集成到SwiftUI项目中时遇到了麻烦。

我将所有正确的API密钥添加到我的AppDelegate.swift文件中,并创建了一个名为GoogMapView的视图,我正在尝试使用它来显示Google地图的实例。这是我在文件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
}

我在"view = mapView"处不断收到错误,但我尝试的所有方法都失败了。知道如何设置它以便我可以在主视图中调用它吗?

我也是一个新的iOS开发人员。我正在研究同样的事情,偶然发现了你的问题。由于我是iOS的新手,我不能声称它遵循所有正确的约定,但是此代码可以在基本级别使用SwiftUI在模拟器上显示地图。解决方案基于您的代码和 Matteo 的观察。

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
}
}

我遇到了这个问题,所以我更改了参数以匹配。

func updateUIView(_ **view** GMSMapView, context: Self.Context) {
marker.map = **view** }

相关内容

  • 没有找到相关文章

最新更新