【问题标题】:Swift 3 Firebase to MapKitSwift 3 Firebase 到 MapKit
【发布时间】:2017-03-20 20:30:16
【问题描述】:

我希望将数据从 Firebase 数据库获取到地图中。我有很多工作,但被卡住了。这都是 100% 的代码,没有故事板。

我在下面的作品。它会在地图上显示所有图钉,但我被困在了这一点上。我希望能够点击每个引脚并获取该特定引脚的数据。当我这样做时,使用下面的代码我将打印出“tap”和 MTA 的数组。

数据可以显示在引脚注释/信息窗口或视图控制器中地图下方的标签中。我不确定将代码放在哪里/让它工作。我假设不在快照中,但我无法为每个单独的记录/引脚获取数据。

视图已加载以供参考:

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        self.navigationItem.title = stop

        mapContainerView.delegate = self
        view.addSubview(mapContainerView)


        setUpContorller()
        fetchTrip()
}

获取地图坐标的函数:

    func fetchTrip(){

    let ref = FIRDatabase.database().reference()
    let tripsRef = ref.child("Trips").child(stop!)
    tripsRef.observeSingleEvent(of: .value, with: { (snapshot) in

        for snap in snapshot.children {
            let tripSnap = snap as! FIRDataSnapshot

            if let dict = tripSnap.value as? [String:AnyObject] {
                let lat = dict["lat"] as! CLLocationDegrees
                let lng = dict["lng"] as! CLLocationDegrees
                let MTA = dict["MTAStop"] as! String
                let center = CLLocationCoordinate2D(latitude: lat, longitude: lng)
                let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.08, longitudeDelta: 0.08))

                self.stopMTA.append(MTA)

                self.mapContainerView.setRegion(region, animated: true)

                let pinCoordinate: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, lng)

                let annotation = MKPointAnnotation()
                annotation.coordinate = pinCoordinate
                self.mapContainerView.addAnnotation(annotation)

            }
        }
    })
}

点击 pin 的功能:(我知道我需要更多......不确定是什么,可能是一个保存数据的类。)

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    // do something
    print("tap")
    print(stopMTA)
}

这里是模拟器测试的数据库示例。

提前致谢!

【问题讨论】:

    标签: ios firebase swift3 firebase-realtime-database mapkit


    【解决方案1】:

    使用 MKPointAnnotation() 只会在地图上放置一个图钉。您需要使用自定义类来保存注释信息并使用初始化程序调用它。

    原文:

    let annotation = MKPointAnnotation()
    annotation.coordinate = pinCoordinate
    self.mapContainerView.addAnnotation(annotation)
    

    工作版本:

    let annotation = PinAnnotation(title: MTA, coordinate: pinCoordinate, info: MTA)
    annotation.coordinate = pinCoordinate
    self.mapContainerView.addAnnotation(annotation)
    

    还需要:

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    }
    

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      相关资源
      最近更新 更多