gpt4 book ai didi

swift - 当选择谷歌地图标记时将数据传递给 VC Swift

转载 作者:行者123 更新时间:2023-11-30 13:47:56 25 4
gpt4 key购买 nike

我在我的iOS应用程序(Swift中)中使用Google Maps SDK,它在用户搜索的位置上显示一个标记(使用Google Places API),当用户选择该标记时,它会显示一个新的 View Controller 到屏幕上,我遇到的问题是将数据(标记的坐标)传递到新的 View Controller (为了论证,该 Controller 希望将它们打印到控制台)。

以下是在 map 上放置制造商的代码(我使用的是 GooglePlaceAutocomplete - https://github.com/watsonbox/ios_google_places_autocomplete ):

// Allows user to search in search box from googlePlacesApi, when place is selected marker is placed
func placeSelected(place: Place) {
var latitude: Double = 0.0
var longitude: Double = 0.0
place.getDetails { details in
latitude = details.latitude
longitude = details.longitude

let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
self.mapView.camera = GMSCameraPosition(target: location, zoom: 15, bearing: 0, viewingAngle: 0)
self.placeViewClosed()
self.placeMarker(location)
}

下面是 Google map View Controller 中的代码,用于在选择标记时显示新 View :

// executes when user taps custom window info above marker, presents PopooverViewController
func mapView(mapView: GMSMapView!, didTapInfoWindowOfMarker marker: GMSMarker!) {
let popoverContent = (self.storyboard?.instantiateViewControllerWithIdentifier("Popover"))! as UIViewController
let nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
let popover = nav.popoverPresentationController
popoverContent.preferredContentSize = CGSizeMake(250,300)
popover!.delegate = self
popover!.sourceView = self.view
popover!.sourceRect = CGRectMake(100,100,0,0)
self.presentViewController(nav, animated: true, completion: nil)
}

我尝试过使用协议(protocol),但我似乎无法传递数据!如果有人知道(在 Swift 中)如何传递数据,我们将不胜感激!

编辑

在我有趣的地方选择我现在使用 NSUserDefaults,例如:

func placeSelected(place: Place) {
var latitude: Double = 0.0
var longitude: Double = 0.0
place.getDetails { details in
latitude = details.latitude
longitude = details.longitude
NSUserDefaults.standardUserDefaults().setObject(longitude, forKey: "longitude")
NSUserDefaults.standardUserDefaults().setObject(latitude, forKey: "latitude")
let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
self.mapView.camera = GMSCameraPosition(target: location, zoom: 15, bearing: 0, viewingAngle: 0)
self.placeViewClosed()
self.placeMarker(location)
}

我正在使用我的新 VC:

let latitude:Double =  NSUserDefaults.standardUserDefaults().objectForKey("latitude") as! Double
let longitude:Double = NSUserDefaults.standardUserDefaults().objectForKey("longitude") as! Double

这是可以接受的做法吗?

最佳答案

我建议创建一个 segue 并以编程方式触发它:

func mapView(mapView: GMSMapView!, didTapInfoWindowOfMarker marker: GMSMarker!)

您可以使用以下方式触发它:

performSegueWithIdentifier("SEGUE_ID", sender: self)

然后在此处的 View Controller 中设置您想要的任何属性:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) 
{
let viewController = segue.destinationViewController
}

关于swift - 当选择谷歌地图标记时将数据传递给 VC Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34693904/

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