Marker didtap无法将数据传递给View Controller

时间:2017-06-05 07:40:21

标签: ios google-maps swift3 marker

当点击视图上的标记弹出一个视图控制器以显示标记的细节时。 但是现在我遇到的问题是数据没有传递给View Controller。 我正在使用循环来循环我的标记。

这是我的谷歌地图视图控制器:

class locationViewController: UIViewController, GMSMapViewDelegate {

@IBOutlet weak var googleMapView: UIView!

@IBOutlet weak var menuView: UIView!

var mapView:GMSMapView?
var popUptitle : String?
var popupaddress : String?
var popuptime : String?
var popupphone : String?

struct State {
    let name: String
    let long: CLLocationDegrees
    let lat: CLLocationDegrees
    let phone : String
    let address : String
    let time : String
}

override func viewDidLoad() {

    super.viewDidLoad()


    let A = GMSCameraPosition.camera(withLatitude: 2.1898683, longitude: 102.2464156, zoom: 13)
    mapView = GMSMapView.map(withFrame:self.view.bounds, camera: A)
    mapView?.delegate = self

    let states = [
        State(name: "A", long: 102.2464156, lat: 2.1898683,phone: "1234", address: "A Address",time:"11AM - 10PM"),
        State(name: "B", long: 102.249949, lat: 2.189052,phone: "Tel No: 5678", address: "B Address",time:"11AM - 10PM"),
        State(name: "C", long: 102.252837, lat: 2.205183,phone: "Tel No: 9012", address: "C Address",time:"11AM - 10PM")

    ]
    for state in states {
        let markerPosition = CLLocationCoordinate2DMake(state.lat, state.long)
        let marker = GMSMarker(position:markerPosition)
        marker.icon = UIImage(named: "custom_marker")
        marker.map = mapView
        popUptitle = state.name
        popupaddress = state.address
        popupphone = state.phone
        popuptime = state.time
    }

    self.googleMapView.addSubview(mapView!)
    self.mapView?.addSubview(menuView)
    menuView.isHidden = true
    }

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    let popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "marker_popup") as! MarkerPopupViewController
    self.addChildViewController(popup)
    popup.view.frame =  self.view.bounds
    self.view.addSubview(popup.view)
    popup.didMove(toParentViewController: self)
    popup.marker_name = popUptitle
    popup.marker_address = popupaddress
    popup.marker_time = popuptime
    popup.marker_pn = popupphone
    return true
}

这是我的自定义信息窗口视图控制器

import UIKit

class MarkerPopupViewController: UIViewController {

var marker_name : String?
var marker_address : String?
var marker_time : String?
var marker_pn : String?

@IBOutlet weak var outlet_pn: UILabel!
@IBOutlet weak var outlet_time: UILabel!
@IBOutlet weak var outlet_address: UILabel!
@IBOutlet weak var outlet_name: UILabel!
@IBOutlet weak var outlet_img: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()
    self.outlet_name.text = marker_name
    self.outlet_time.text = marker_time
    self.outlet_address.text = marker_address
    self.outlet_pn.text = marker_pn
    self.outlet_address.sizeToFit()
}
@IBAction func marker_close(_ sender: Any) {
    self.view.removeFromSuperview()
}

@IBAction func callBtn(_ sender: Any) {
}
@IBAction func directionBtn(_ sender: Any) {
}


}

1 个答案:

答案 0 :(得分:1)

将行的顺序更改为

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    let popup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "marker_popup") as! MarkerPopupViewController
    popup.marker_name = popUptitle
    popup.marker_address = popupaddress
    popup.marker_time = popuptime
    popup.marker_pn = popupphone
    popup.view.frame =  self.view.bounds
    self.addChildViewController(popup)
    self.view.addSubview(popup.view)
    popup.didMove(toParentViewController: self)
    return true
}