2017-09-26 26 views
2

この質問のバリエーションを見たことがありますが、私のコードをどのように設定するかについては答えがありません。私は解決策に困惑しています。私はこのプロジェクトを直ちに完了させるために立ち寄っています。それで、私は救いの手を求めています。私がやろうとしている何MapにFirebaseデータを埋め込む

はこれです:

は次のように設定私のFirebase配列、から座標を引い:FireBase Setup

私のメインフィードは、このようなものです:

// 
 
// 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を作ってそこにデータを入れるべきですか?私は何を変更するか分からないが、私は銃の下にあり、私は助けが必要です!

+0

後に次のコードを追加します。 MKPointAnnotationはカスタム注釈を許可しません。より具体的でないことをお詫びします。 –

答えて

4

は、私は、カスタム注釈にこれを有効にする方法を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) 
+0

ありがとうございました!!!私はついにこれを手に入れました。 –

関連する問題