我在我的iOS应用程序(在Swift中)使用Google Maps SDK,该应用程序在用户搜索的位置(使用Google Places API)上显示标记,当用户随后选择该标记时,它会呈现新的视图控制器在屏幕上,我遇到的问题是将数据(标记的坐标)传递给新的View控制器(为了参数起见,想要将它们打印到控制台)。
以下是将制作者放置在地图上的代码(我使用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地图视图控制器中的代码,用于在选择标记时显示新视图:
// 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)
}
我尝试过使用协议,但我似乎无法传递数据!如果有人有任何想法(在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
这是一种可以接受的方式吗?
答案 0 :(得分:0)
我建议创建一个segue并以编程方式触发它:
func mapView(mapView: GMSMapView!, didTapInfoWindowOfMarker marker: GMSMarker!)
您可以使用以下方式触发:
performSegueWithIdentifier("SEGUE_ID", sender: self)
然后在视图控制器中设置您想要的任何属性:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
{
let viewController = segue.destinationViewController
}