【发布时间】:2017-02-23 12:29:24
【问题描述】:
我是 swift 和 Xcode 的初学者,我的代码需要一些帮助。我制作了一个firebase数据库:
我有一个地图视图,并且已经对地图上的每个注释进行了编码,只需要有人告诉我如何读取数据库中的所有位置,然后将它们作为注释放在地图视图上。到目前为止,这是我所有的代码:
import UIKit
import MapKit
import FirebaseDatabase
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
map.showsUserLocation = true
// Do any additional setup after loading the view.
map.mapType = .satellite
self.map.isZoomEnabled = false
map.showsCompass = false
map.isRotateEnabled = false
let latitude: CLLocationDegrees = 51.743370
let longitude: CLLocationDegrees = -2.279179
let lanDelta: CLLocationDegrees = 0.05
let lonDelta: CLLocationDegrees = 0.05
let span = MKCoordinateSpan(latitudeDelta: lanDelta, longitudeDelta: lonDelta)
let coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let region = MKCoordinateRegion(center: coordinates, span: span)
map.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.title = "School House"
annotation.subtitle = "Main School Building"
annotation.coordinate = coordinates
map.addAnnotation(annotation)
map.selectAnnotation(annotation, animated: true)
let annotationCollingwood = MKPointAnnotation()
annotationCollingwood.title = "Collingwood"
annotationCollingwood.subtitle = "Day House"
annotationCollingwood.coordinate = CLLocationCoordinate2D(latitude: 51.742688, longitude: -2.279362)
map.addAnnotation(annotationCollingwood)
let annotationSibly = MKPointAnnotation()
annotationSibly.title = "Sibly"
annotationSibly.subtitle = "Musical Theatre"
annotationSibly.coordinate = CLLocationCoordinate2D(latitude: 51.743518, longitude: -2.279796)
map.addAnnotation(annotationSibly)
map.camera.altitude = 300.00
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func menuButton(_ sender: Any) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewController(withIdentifier: "MenuViewController") as! MenuViewController
self.present(resultViewController, animated:true, completion:nil)
}
}
【问题讨论】:
-
你能展示你尝试过的firebase吗?
-
Joe - 决定你想问什么,然后问那个。您是否有关于在 Firebase 中存储数据、检索数据或在地图上进行注释的问题?如果您真正要问的是“您如何使用 Firebase?”然后从 google 和众多可用于解决该问题的教程之一开始
-
您好,抱歉,我没有说得这么清楚。我已经将数据添加到他们网站上的 firebase 数据库中,所以我对此没有任何问题。我只是不知道如何检索位置及其态度和经度,然后将它们全部显示为地图上的注释。我也在努力做到这一点,所以当您向 firebase 数据库添加新位置时,它会在您下次加载地图时出现。我看了很多教程,但不知道如何在我的中实现代码,所以我只是要求一些示例。
-
请将问题中的图片替换为您的 Firebase 结构和规则的文本数据。这样我们就不必在答案中重新输入它,它可以搜索并且更短。
标签: ios firebase swift3 firebase-realtime-database