Swift 2向注释添加标注

时间:2016-02-23 18:44:21

标签: swift2 mapkit xcode7 callouts

我的注释中添加了标注,我遇到了极大的困难。我已从以下代码中删除了添加标注的任何尝试。

注释添加在updateVisiblePins函数的底部

import UIKit
import MapKit
import CoreLocation
import Foundation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
    @IBOutlet weak var mapView: MKMapView!
    var locationManager = CLLocationManager()
    var landmarkToPass: String!
    var rowIndex: Int!


    struct MyVars {
        static var prevLoc = CLLocation()
        static var region = MKCoordinateRegion()
        static var landmarks = [Landmark]()
        static var landmark: Landmark = Landmark(title: String(), locationName: String(), discipline: String(), coordinate: CLLocationCoordinate2D())
}

    override func viewDidLoad() {
        super.viewDidLoad()

        self.locationManager = CLLocationManager()
        self.locationManager.delegate = self
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
        self.mapView.showsUserLocation = true
        var landmarks: [Landmark] = [Landmark]()
        loadInitialData()
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            var prevLoc = MyVars.prevLoc
            let userLoction: CLLocation = locations[0]
            let distance = calculateDisatnceBetweenTwoLocations(prevLoc, destination: userLoction)
            if prevLoc != userLoction {
                prevLoc = userLoction
                MyVars.prevLoc = userLoction

            if distance > 5 {
                let latitude = userLoction.coordinate.latitude
                let longitude = userLoction.coordinate.longitude
                let latDelta: CLLocationDegrees = 0.05
                let lonDelta: CLLocationDegrees = 0.05
                let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
                let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
                MyVars.region = MKCoordinateRegionMake(location, span)
                self.mapView.showsUserLocation = true
                self.mapView.setRegion(MyVars.region, animated: true)
                updateVisiblePins()
            } else {
                let latitude = userLoction.coordinate.latitude
                let longitude = userLoction.coordinate.longitude
                let span = mapView.region.span
                let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
                MyVars.region = MKCoordinateRegionMake(location, span)
                self.mapView.showsUserLocation = true
                updateVisiblePins()
            }
        }
    }


    func calculateDisatnceBetweenTwoLocations(source:CLLocation,destination:CLLocation) -> Double{
        var distanceMeters = source.distanceFromLocation(destination)
        var distanceKM = distanceMeters / 1000
        return distanceKM
    }


    func updateVisiblePins() {
        for (index, landmark) in MyVars.landmarks.enumerate() {
            let landmarkLat = landmark.coordinate.latitude
            let landmarkLon = landmark.coordinate.longitude
            let userLocation = locationManager.location
            let landmarkLocation = CLLocation(latitude: landmarkLat, longitude: landmarkLon)
            let distance = calculateDisatnceBetweenTwoLocations(userLocation!, destination: landmarkLocation)
            if distance < 30 {
                mapView.addAnnotation(landmark)
            } else {
                if rowIndex != nil {
                    if index == rowIndex{
                        self.mapView.addAnnotation(landmark)
                    } else {
                        mapView.removeAnnotation(landmark)
                    }
                }
            }
        }
    }


    func loadInitialData() {
        // 1
        let fileName = NSBundle.mainBundle().pathForResource("PublicLandmark", ofType: "json")

        var data: NSData = NSData()
        do {
            data  = try NSData(contentsOfFile: fileName!, options: [])
        } catch {

        }


        // 2
        var jsonObject: AnyObject!
        do {
            jsonObject = try NSJSONSerialization.JSONObjectWithData(data,
                options: [])
        } catch {

        }

        // 3
        if let jsonObject = jsonObject as? [String: AnyObject],

        // 4
        let jsonData = JSONValue.fromObject(jsonObject)?["data"]?.array {
            for landmarkJSON in jsonData {
                if let landmarkJSON = landmarkJSON.array,landmark = Landmark.fromJSON(landmarkJSON) {
                        MyVars.landmarks.append(landmark)
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

需要将mapView.delegat = self行添加到viewDidLoad()函数中。