In my console, I can access Firebase latitude, longitude, and name of restaurant. I have a few print statements that I use to test that I am getting the values I needed. When I try to assign these values into an annotation.coordinate = CLLocationCoordinate2DMake(with the respective info here)
I still can't get this function to print in maps.
I mainly created a function so that I can call it in viewDidLoad()
so that everything I want automatically comes up when this page of the app loads.
I also created an action for a button so that when a user clicks a button on shown on the view controller, it also prints the location but THAT case also is not working. When I make coordinates I also do not know if I need ! on res.latitude
and res.longitude!
…. When I take it off it still does not work. A previous project I created I added annotations the same way I did here MINUS the firebase array. I created one myself with struct variables (title
, latitude
, longitude
) in them that are later called.
import UIKit
import Firebase
import FirebaseStorage
import FirebaseDatabase
import MapKit
import CoreLocation
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {//add last 2 delegates/protocols.conforming
//firebase refrences
var dataBaseRef: FIRDatabaseReference! {
return FIRDatabase.database().reference()
}
var storageRef: FIRStorageReference! {
return FIRStorage.storage().reference()
}
var restaurantArray = [Restaurant]()
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var segments: UISegmentedControl!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
title = "Maps"
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Main Menu", style: .plain, target: self, action: #selector(SSASideMenu.presentLeftMenuViewController))
self.locationManager.delegate = self//as soon as loaded find location--conforms to delegate
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest//best location
self.locationManager.requestWhenInUseAuthorization()//only want location when using app
self.locationManager.startUpdatingLocation()//turn on location manager..make location start looking
self.mapView.showsUserLocation = true//shows blue dot
displayRestaurants()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
fetchRestaurants()
displayRestaurants()
}
//see if you want to give it a try!!!!!!!!!!!!!!!
func fetchRestaurants(){
FIRDatabase.database().reference().child("AthensRestaurants/Restaurants").observe(.value, with: { (snapshot) in
var results = [Restaurant]()
for res in snapshot.children{
let res = Restaurant(snapshot: res as! FIRDataSnapshot)
results.append(res)
}
self.restaurantArray = results
}) { (error) in
print("error encountered dumbass")
print(error.localizedDescription)
}
}
//work here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
func displayRestaurants(){
//array already created--using a firebase array
for res in restaurantArray {
let annotation = MKPointAnnotation()
annotation.title = res.name
print(res.name)
let x = res.latitude //shows that this works and I can retrieve data!!!
print (x! as Double)
let y = res.longitude
print(y! as Double)
annotation.coordinate = CLLocationCoordinate2D(latitude: res.latitude!, longitude: res.longitude!) //Should the exclamation marks be there "!"
mapView.addAnnotation(annotation)
}
}
//A way of testing
@IBAction func test12(sender: UIButton) {
displayRestaurants() //another way i tried makiing anotations show....!!!!!!!!!
}
//below works on seperate projects
//MARK: - Location Delegate Methods
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {//didupdate is contiously called so below is continuously called
let location = locations[0]
let span = MKCoordinateSpanMake(0.01, 0.01)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region = MKCoordinateRegionMake(myLocation, span)//lat long--region that we want map to scope to--parameters is closeness zoom
self.mapView.setRegion(region, animated: true)//since we have thise we can stop updating eventually
self.locationManager.stopUpdatingLocation()
}
//check for errors
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {//should be NSError but
print("Errors:" + error.localizedDescription)
//displayRestaurants()
}
//segment changer for terrain, hybrid, and regular---just allows different types of map views
@IBAction func segChange(_ sender: Any) {
switch segments.selectedSegmentIndex {
case 0:
mapView.mapType = MKMapType.standard
break
case 1:
mapView.mapType = MKMapType.satellite
break
case 2:
mapView.mapType = MKMapType.hybridFlyover
break
default:
break
}
}
}