Swift 3 - 每次添加位置时都会加载UIView

时间:2018-01-02 10:56:59

标签: ios swift firebase mkmapview

我使用firebase作为我的后端。在这个应用程序中,我使用了mapkit和firebase。我想跟踪用户的位置。但是当我在做的时候,用户的位置已成功上传到firebase,但在我的应用程序中,视图已重新加载。

我的代码: -

 @IBOutlet weak var map:MKMapView!
let locationManager = CLLocationManager()
var lat = [Double]()
var lon = [Double]()
var ref:DatabaseReference!
var uid = ""

override func viewDidLoad() {
    super.viewDidLoad()
    map.delegate = self
    locationManager.delegate = self
    ref = Database.database().reference().child("users").child(uid)
    ref.child("locations").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in
        print("func snapshot \(snapshot)")
        let dict = snapshot.value as! [String:Double]
        let lat = dict["latitude"]
        let lon = dict["longitude"]

        print("latidue = \(lat!)")
        print("longitude = \(lon!)")

        self.lat.append(lat!)
        self.lon.append(lon!)
    })
}
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let location = CLLocationCoordinate2D(latitude: lat.first!,longitude: lon.first!)
    let destinationLocation = CLLocationCoordinate2D(latitude: lat.last!, longitude:lon.last!)
    let sourcePlacemark = MKPlacemark(coordinate: location, addressDictionary: nil)
    let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
    let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
    let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
    let sourceAnnotation = MKPointAnnotation()
    if let location = sourcePlacemark.location {
        sourceAnnotation.coordinate = location.coordinate
    }
    let destinationAnnotation = MKPointAnnotation()
    if let location = destinationPlacemark.location {
        destinationAnnotation.coordinate = location.coordinate
    }
    let directionRequest = MKDirectionsRequest()
    directionRequest.source = sourceMapItem
    directionRequest.destination = destinationMapItem
    directionRequest.transportType = .automobile
    let directions = MKDirections(request: directionRequest)
    directions.calculate {
        (response, error) -> Void in
        guard let response = response else {
            if let error = error {
                print("Error: \(error)")
            }
            return
        }
        let route = response.routes[0]
        self.map.add((route.polyline), level: MKOverlayLevel.aboveRoads)

        let rect = route.polyline.boundingMapRect
        self.map.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
    }

    let span = MKCoordinateSpanMake(0.05, 0.05)
    let region = MKCoordinateRegion(center: location, span: span)
    map.setRegion(region, animated: true)

    let annotation = MKPointAnnotation()
    annotation.coordinate = location

    map.addAnnotation(annotation)
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let renderer = MKPolylineRenderer(overlay: overlay)
    renderer.strokeColor = UIColor.red
    renderer.lineWidth = 4.0

    return renderer
}

我不想重新加载我的整个视图,只是想在上传新的cordinate时渲染我的mapview。

1 个答案:

答案 0 :(得分:0)

viewDidAppear 中删除代码并将其放入observe childAdded回调中