gpt4 book ai didi

Ios 如何将谷歌地图用户数据传递到下一个 View

转载 作者:行者123 更新时间:2023-11-28 06:06:04 26 4
gpt4 key购买 nike

当用户点击自定义信息窗口时,我试图将谷歌地图的 userData 传递到下一个 View 。

首先我像这样创建模型对象。

import UIKit
import Firebase

struct Team {

var key: String
var teamName: String
var league: String
var lat: Double
var lng: Double

init(snapshot: DataSnapshot) {

self.key = snapshot.key
self.teamName = (snapshot.value as! NSDictionary)["teamName"] as? String
self.league = (snapshot.value as! NSDictionary)["league"] as? String ?? ""
self.lat = (snapshot.value as! NSDictionary)["lat"] as? Double ?? 0
self.lng = (snapshot.value as! NSDictionary)["lng"] as? Double ?? 0
}
}

我获取了数据库并像这样放置了userData

var teams = [Team?]()

func fetchTeams(){

let teamRef = Database.database().reference().child("teams")
teamRef.observe(.value, with: { (snapshot) in
var result = [Team]()

for child in snapshot.children {
let child = Team(snapshot: child as! DataSnapshot)
result.append(child)
self.teams = result
}

for team in self.teams {
guard let lat = team?.lat else { return }
guard let lng = team?.lng else { return }
let marker: GMSMarker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(lat, lng)
marker.map = self.mapView
marker.userData = team
}

}, withCancel: nil)
}

这个 func 工作得很好,所以我猜 userData 有它的正确属性。

func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let infoWindow = Bundle.main.loadNibNamed("Marker", owner: self, options: nil)?.first as! MarkerView

infoWindow.teamLabel.text = (marker.userData as! Team).teamName

return infoWindow
}


func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
self.performSegue(withIdentifier: cellId, sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == cellId {
let next: NextViewController = segue.destination as! NextViewController
let marker = GMSMarker()
next.team = marker.userData as? Team
}
}

我像上面那样实现了将数据传递给 NextViewController。它在 performSegue 之前工作正常,但 next.team 为零。有谁知道为什么?提前致谢!

最佳答案

您遇到这个问题是因为您正在创建一个空的 GMSMarker,当然这个空的 GMSMarker 没有任何用户数据,您必须传递选定的 GMSMarker 作为 performSegue 中的参数,特别是在 sender 参数中,并转换为 Team 并将其传递给您的 NextViewController

func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
self.performSegue(withIdentifier: cellId, sender: marker.userData)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == cellId {
let next: NextViewController = segue.destination as! NextViewController
if let teamData = sender as? Team{
next.team = teamData
debugPrint("teamData is fine")
}
debugPrint("segue identifier is correct")
}
}

关于Ios 如何将谷歌地图用户数据传递到下一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48170133/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com