我刚开始使用 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) …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试使用marker.userData 属性在Google 地图markerInfoWindow 中动态显示有关其特定位置的数据。
这是我设置marker.userData 并将标记放在我的地图实例上的函数:
func putPlaces(places: [Place]) {
for place in places.prefix(10) {
print("*******NEW PLACE********")
let name = place.name
let address = place.address
let location = ("lat: \(place.geometry.location.latitude), lng: \(place.geometry.location.longitude)")
let locLat = place.geometry.location.latitude
let locLon = place.geometry.location.longitude
let place_id = place.place_id
let photo = place.photos
let types = place.types
let marker : GMSMarker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: locLat, longitude: locLon)
marker.icon = GMSMarker.markerImage(with: .black)
print("PLACE: \(place)")
print("PLACE.NAME: \(place.name)")
marker.userData = ["name" : "names"]
marker.map = self.mapView …Run Code Online (Sandbox Code Playgroud)