0

在我的控制台中,我可以访问 Firebase 纬度、经度和餐厅名称。我有一些打印语句用于测试我是否获得了所需的值。当我尝试将这些值分配给一个时,annotation.coordinate = CLLocationCoordinate2DMake(with the respective info here)我仍然无法让这个函数在地图中打印。

我主要创建了一个函数,以便我可以调用它,viewDidLoad()以便在加载此应用程序页面时自动出现我想要的所有内容。

我还为按钮创建了一个操作,这样当用户单击视图控制器上显示的按钮时,它也会打印位置,但这种情况也不起作用。做坐标的时候也不知道要不要!res.latituderes.longitude!…… _ 当我把它取下来时它仍然不起作用。我之前创建的一个项目添加了注释,就像我在这里做的一样减去 firebase 数组。我自己创建了一个,其中包含结构变量 ( title, latitude, longitude),这些变量稍后会被调用。

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
    }
}   
}
4

0 回答 0