我从Firebase获取坐标,然后我将它们放在我的地图上,但问题是我的Firebase取指令发生在地图初始化标记后,在我的坐标中给出nil
。
如何从Firebase获取值并将其作为标记?
var fetchLat: Double!
var fetchLong: Double!
在这里,我从Firebase获取值
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let camera = GMSCameraPosition.camera(withLatitude: latPass, longitude: longPass, zoom: 5)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
mapView.settings.scrollGestures = true
mapView.settings.zoomGestures = true
mapView.settings.myLocationButton = true
let dataBaseRef=FIRDatabase.database().reference()
dataBaseRef.child("LocationTest").queryOrderedByKey().observe(.childAdded, with: {(snapshot) in
let postDict = snapshot.value as? [String : AnyObject] ?? [:]
var fetchLat = postDict["lat"] as! Double
var fetchLong = postDict["long"] as! Double
})
我将它们放在地图上
let friendLocator = [
Locator(name: "Virat", long: fetchLong, lat: fetchLat),
]
for friend in friendLocator{
let friendMarker = GMSMarker()
friendMarker.position=CLLocationCoordinate2D(latitude: friend.lat, longitude: friend.long)
friendMarker.title=friend.name
friendMarker.map=mapView
mapView.selectedMarker=friendMarker
}
由于地图初始化是在从Firebase获取值之前完成的,因此如何在获取值后设置标记?
答案 0 :(得分:1)
如果在地图上显示用户的ViewController与LocationManager
的委托相同,那么
在ViewController中创建一个属性
var friendLocator : [Locator] = [Locator]()
创建一个在地图上绘制用户的功能
func locateFriends() {
if self.view is GMSMapView {
for friend in friendLocator {
let friendMarker = GMSMarker()
friendMarker.position=CLLocationCoordinate2D(latitude: friend.lat, longitude: friend.long)
friendMarker.title=friend.name
friendMarker.map= (self.view as! GMSMapView)
(self.view as! GMSMapView).selectedMarker=friendMarker
}
}
}
最后将新找到的定位器实例附加到didUpdateLocations
委托
extension ViewController : UIPageViewControllerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let dataBaseRef=FIRDatabase.database().reference()
dataBaseRef.child("LocationTest").queryOrderedByKey().observe(.childAdded, with: {(snapshot) in
let postDict = snapshot.value as? [String : AnyObject] ?? [:]
var fetchLat = postDict["lat"] as! Double
var fetchLong = postDict["long"] as! Double
let locator = Locator(name: "Virat", long: fetchLong, lat: fetchLat)
//do some sort of duplicate check before blindly adding new locator to array
self.friendLocator.append(locator)
self.locateFriends()
})
}
}
如果在地图上显示用户的ViewController不同,那么UILocationManager委托的那个使用相同的逻辑,而是使用delegate
模式通知ViewController新的定位器对象并重新加载地图。
修改强>
重新格式化OP的代码
import MapKit
import UIKit
import CoreLocation
import GoogleMaps
import GooglePlaces
import GoogleMapsCore
import Firebase
import FirebaseDatabase
struct postStruct{
let lat: Double!
let long: Double!
}
class ViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate, UISearchBarDelegate{
var viratPin=CustomPointAnnotation()
var posts=[postStruct]()
var mapView : GMSMapView? = nil
var friendLocator : [Locator] = [Locator]()
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var searchSupporter: UIView!
@IBAction func dismissKeyboard(_ sender: Any) {
searchBar.resignFirstResponder()
}
struct Locator {
let name: String
let long: CLLocationDegrees
let lat: CLLocationDegrees
}
class CustomPointAnnotation: MKPointAnnotation {
var imageName: String!
}
let manager = CLLocationManager()
var myLocation: CLLocationCoordinate2D?
var friend1: CLLocationCoordinate2D?
var arbokPin = CustomPointAnnotation()
var location=0
var latPass: Double!
var longPass: Double!
var fetchLat: Double!
var fetchLong: Double!
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
var location=locations[0]
let span:MKCoordinateSpan=MKCoordinateSpanMake(0.01, 0.01)
var myLocation:CLLocationCoordinate2D=CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion=MKCoordinateRegionMake(myLocation, span)
latPass=location.coordinate.latitude
longPass=location.coordinate.longitude
post()
self.configureMapView()
let dataBaseRef=FIRDatabase.database().reference()
dataBaseRef.child("LocationTest").queryOrderedByKey().observe(.childAdded, with: {(snapshot) in
let postDict = snapshot.value as? [String : AnyObject] ?? [:]
var fetchLat = postDict["lat"] as! Double
var fetchLong = postDict["long"] as! Double
let locator = Locator(name: "You", long: fetchLong, lat: fetchLat)
self.friendLocator.append(locator)
self.locateFriend()
})
manager.stopUpdatingLocation()
self.view = mapView
}
func configureMapView() {
let camera = GMSCameraPosition.camera(withLatitude: latPass, longitude: longPass, zoom: 5)
self.mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
mapView.settings.scrollGestures = true
mapView.settings.zoomGestures = true
mapView.settings.myLocationButton = true
mapView.addSubview(searchBar)
mapView.addSubview(searchSupporter)
mapView.bringSubview(toFront: searchBar)
for gesture in mapView.gestureRecognizers! {
mapView.removeGestureRecognizer(gesture)
}
}
func locateFriend() {
for friend in friendLocator{
let friendMarker = GMSMarker()
friendMarker.position=CLLocationCoordinate2D(latitude: friend.lat, longitude: friend.long)
friendMarker.title=friend.name
friendMarker.map=mapView
mapView.selectedMarker=friendMarker
if friend.name=="Virat"{
friendMarker.icon=UIImage(named: "ViratPin.png")
}
else if friend.name=="Naveen"{
friendMarker.icon=UIImage(named: "naveenPin.png")
}
else if friend.name=="You"{
friendMarker.icon=UIImage(named: "currentLocation.png")
}
}
do {
mapView.mapStyle = try GMSMapStyle(jsonString: kMapStyle)
} catch {
NSLog("One or more of the map styles failed to load. \(error)")
}
}
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate=self
manager.desiredAccuracy=kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
searchBar.isUserInteractionEnabled=true
searchBar.delegate=self
searchSupporter.alpha=0
}
func searchBarTextDidBeginEditing(_ searchBar : UISearchBar){
self.searchSupporter.alpha=0.8
print("yes")
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchSupporter.alpha=0
print("no")
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
print(searchBar.text!)
}
override var prefersStatusBarHidden: Bool{
return true
}
func post(){
let post : [String: Double]=["lat":latPass, "long":longPass]
let dataBaseRef=FIRDatabase.database().reference()
//dataBaseRef.child("Location").childByAutoId().setValue(post)
dataBaseRef.child("Location").child("id").setValue(post)
}
}