使用 Firebase 数据填充地图
Posted
技术标签:
【中文标题】使用 Firebase 数据填充地图【英文标题】:Populate Map With Firebase data 【发布时间】:2017-09-26 02:41:18 【问题描述】:我见过这个问题的变体,但没有适用于我如何设置代码的答案。我对解决方案感到困惑,我需要立即完成这个项目。所以,我请求帮助。
我想要做的是:
从我的 Firebase 数组中提取坐标,设置如下:FireBase Setup
我的主要 Feed 是这样的:
//
// FeedVC.swift
// GotToGo
//
// Created by HSI on 9/3/17.
// Copyright © 2017 HSI. All rights reserved.
//
import UIKit
import MapKit
import Firebase
import CoreLocation
class FeedVC: UIViewController, UITableViewDelegate, UITableViewDataSource, MKMapViewDelegate, CLLocationManagerDelegate
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
var centerMapped = false
var geoFire: GeoFire!
var posts = [Post]()
override func viewDidLoad()
super.viewDidLoad()
mapView.delegate = self
mapView.userTrackingMode = MKUserTrackingMode.follow
tableView.delegate = self
tableView.dataSource = self
DataService.ds.REF_VENUE.observe(.value, with: (snapshot) in
self.posts = [] // THIS IS THE NEW LINE
if let snapshot = snapshot.children.allObjects as? [DataSnapshot]
for snap in snapshot
if let postDict = snap.value as? Dictionary<String, AnyObject>
let key = snap.key
let post = Post(postKey: key, postData: postDict)
self.posts.append(post)
if let locationDict = snap.value as? Dictionary<String, AnyObject>
let lat = locationDict["LATITUDE"] as! CLLocationDegrees
let long = locationDict["LONGITUDE"] as! CLLocationDegrees
let center = CLLocationCoordinate2D(latitude: lat, longitude: long)
_ = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.10, longitudeDelta: 0.10))
print(lat,long)
self.tableView.reloadData()
)
//Brings up the user on the map after authorization
override func viewDidAppear(_ animated: Bool)
super.viewDidAppear(animated)
locationAuthStatus()
//Checks if app is authorized to get user's location data.
func locationAuthStatus ()
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse
mapView.showsUserLocation = true
else
locationManager.requestWhenInUseAuthorization()
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
if status == .authorizedWhenInUse
mapView.showsUserLocation = true
func centerMapOnLocation(location: CLLocation)
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 1000, 1000)
mapView.setRegion(coordinateRegion, animated: true)
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation)
if let loc = userLocation.location
if !centerMapped
centerMapOnLocation(location: loc)
centerMapped = true
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
var annotationView: MKAnnotationView?
if annotation.isKind(of: MKUserLocation.self)
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "User")
annotationView?.image = UIImage(named: "icon")
return annotationView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return posts.count
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
if let cell = tableView.dequeueReusableCell(withIdentifier: "detailCell", for: indexPath) as? PostCell
let cellData = posts[indexPath.row]
cell.configureCell(post: cellData)
return cell
else
return PostCell()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
let post = posts[indexPath.row]
performSegue(withIdentifier: "previewSegue", sender: post)
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if let destination = segue.destination as? PreviewVC
if let update = sender as? Post, let update2 = sender as? Post
destination.locationData = update
destination.addressData = update2
我的代码运行,我可以打印纬度和经度,但我不知道如何将它们添加到地图中。我应该再做一个VC并将数据放在那里吗?我不知道要改变什么,但是,我已经迫不及待了,我需要帮助!
【问题讨论】:
如何将其转换为自定义注释。 MKPointAnnotation 不允许自定义注释。我很抱歉没有更具体 【参考方案1】:在print(lat,long)
之后添加如下代码
print(lat,long)
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(lat, long)
annotation.title = "Hello!" // if you need title, add this line
self.mapView.addAnnotation(annotation)
【讨论】:
以上是关于使用 Firebase 数据填充地图的主要内容,如果未能解决你的问题,请参考以下文章
Uncaught FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - 在 vue.js 中调用 Fireba
Stripe 和 Firebase:FirebaseError:collection() 的第一个参数应为 CollectionReference、DocumentReference 或 Fireba