使用地图注释按钮在视图之间快速切换

时间:2016-09-03 08:49:22

标签: swift mkmapview xcode7 segue mkannotation

我相对较新的编程,需要通过点击mapView注释引脚中的按钮来切换视图。

import UIKit
import MapKit
import CoreLocation    

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
    //REF

    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var bottompnl: UIImageView!
    @IBAction func mysticsbtn(sender: AnyObject) {
    }
    @IBAction func bondibtn(sender: AnyObject) {
    }

    //MAP
    let locationManager = CLLocationManager()

    //Annotation
    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        let identifier = "beach"
        if annotation is Beach {
            var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
            if annotationView == nil {
                annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                annotationView!.canShowCallout = true
                let btn = UIButton(type: .DetailDisclosure)
                annotationView!.rightCalloutAccessoryView = btn               

            } else {
                annotationView!.annotation = annotation
            }
            return annotationView
            self.performSegueWithIdentifier("mysticssegue", sender: nil)    
        }
        return nil       
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let annotations = getMapAnnotations()

        // Add mappoints to Map
        mapView.addAnnotations(annotations)    
        mapView.delegate = self

        //MAP
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
        //MAKES THE DOT
        self.mapView.showsUserLocation = true  
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.            
    }

    // MAP: Location Delegates Methods
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations.last
        let centre = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
        let region = MKCoordinateRegion(center: centre, span: MKCoordinateSpan(latitudeDelta: 3, longitudeDelta: 3))
        self.mapView.setRegion(region, animated: true)
        self.locationManager.startUpdatingLocation()
    }       

    //TO FIND ERROS
    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Erros: " + error.localizedDescription)           
    }

    //MARK:- Annotations

    func getMapAnnotations() -> [Beach] {
        var annotations:Array = [Beach]()

        //load plist file

        var beaches: NSArray?
        if let path = NSBundle.mainBundle().pathForResource("beaches", ofType: "plist") {
            beaches = NSArray(contentsOfFile: path)
        }
        if let items = beaches {
            for item in items {
                let lat = item.valueForKey("lat") as! Double
                let long = item.valueForKey("long")as! Double
                let annotation = Beach(latitude: lat, longitude: long)
                annotation.title = item.valueForKey("title") as? String
                annotations.append(annotation)
            }
        }          

        return annotations            
    } 
    //END
}

1 个答案:

答案 0 :(得分:0)

在调用mapView之前,您似乎正在从函数performSegueWithIdentifier返回。

return annotationView
self.performSegueWithIdentifier("mysticssegue", sender: nil)

尝试颠倒这两行的顺序。我还注意到你有一些@IBAction方法没有代码。