2016-02-23 2 views
1

J'ai beaucoup de difficulté à ajouter des légendes à mes annotations. J'ai supprimé toute tentative d'ajout de la légende du code ci-dessous.Swift 2 Ajout de légendes aux annotations

Les annotations sont ajoutées au bas des updateVisiblePins fonctionnent

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

Répondre

0

La ligne a besoin mapView.delegat = self à ajouter à viewDidLoad() fonction.