【问题标题】:Passing location information from uibutton to a map view page and start navigating将位置信息从 uibutton 传递到地图视图页面并开始导航
【发布时间】:2019-03-25 01:28:48
【问题描述】:

enter image description here

    
    var location: [Location] = [
    
    Location(title: "LaFarge Hall", cllocation: CLLocation(latitude: 39.992853, longitude: -75.239888), regionRadius: 300.0, location: "Cardinal Ave", type: "Dorm Hall", coordinate: CLLocationCoordinate2DMake( 39.992853, -75.239888))
    ]
    
    @IBAction func tapDidNavi(sender: UIButton){
        
        
    }
    

我有一个按钮列表,这些按钮是用户可以选择的下车地点。当用户单击按钮时,按钮将移动到地图视图页面并绘制从当前位置到他们选择的位置的 google polyliner 路径。放置位置按钮位于包含位置坐标的数组中,我想传递位置坐标并启动一个地图视图,该视图绘制从当前位置到坐标的路径。

【问题讨论】:

  • 您能否提供在您的应用中可以选择这些位置的源代码?
  • 我刚刚包含在源代码中。
  • @A.Welch 我还提供了一个指向我的故事板屏幕截图的链接。有一个按钮列表,我想将这些按钮放在数组中,其中包括位置坐标,以及用户单击按钮的时间;它会将他们引导到地图视图页面,并绘制从当前位置到下车位置的路径。

标签: ios swift tableview


【解决方案1】:

我建议在这里尝试使用 UITableView 而不是按钮列表。但是,如果您需要使用按钮列表,这应该可以:

只需创建一个 UIButton 的子类并将位置属性添加到它的初始化程序。

class LocationButton: UIButton {

public var location : Location

init(location: Location, frame: CGRect) {

    self.location = location

    super.init(frame: frame)

    setProperties()
}

///needed for interface builder 
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

private func setProperties() {
    //Set properties of button here...
    setTitle("Button Title", for: .normal)
   }
}

创建一个“LocationButton”将如下所示:

let locationButton = LocationButton(location: Location(title: "LaFarge Hall", location: CLLocation(), frame: CGRect())

确保还为数组中的每个按钮添加一个目标:

locationButton.addTarget(self, action: #selector(tapDidNavi(_:)), for: .touchUpInside)

更新您的 tapDidNavi() 函数以接受 LocationButton 类型。然后,您将能够从按下的任何按钮中获取位置并将其传递给下一个 viewController,如下所示:

@IBAction func tapDidNavi(sender: LocationButton) {
    let nextViewController = SomeViewController() //This viewController must have a 'public var Location' inside of it.
    nextViewController.location = sender.location //The location object you set on the button

    self.navigationController?.pushViewController(nextViewController, animated: true)
}

【讨论】:

  • 如何让作为地图视图控制器的视图控制器获取位置?在我的地图视图控制器中,我只有属性 var locationManager = CLocationManager()
  • 当我输入 nextViewContrroller.location = sender.location 它不起作用
  • 将此属性添加到您的 mapViewController: var location: Location!
  • 并用你的mapViewController的名字切换“nextViewController”
猜你喜欢
  • 1970-01-01
  • 2020-09-21
  • 1970-01-01
  • 2020-05-27
  • 1970-01-01
  • 1970-01-01
  • 2013-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多