zly*_*lyt 8 xcode google-maps swiftui xcode11
我刚开始使用 Xcode 和 SwiftUI 进行编程,并且在将 Google 地图集成到 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
}
Run Code Online (Sandbox Code Playgroud)
我一直在“view = mapView”处收到错误消息,但我尝试的一切都失败了。知道如何设置它以便我可以在主视图中调用它吗?
小智 7
我也是一个新的 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
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4685 次 |
最近记录: |