2015-10-20 39 views
0

我無法讓我的註釋顯示在我的地圖視圖中。目前沒有任何顯示。我的代碼有什麼問題?註解不會出現在MapView上

這裏是我的基本地圖視圖控制器:

import UIKit 
import MapKit 
import CoreLocation 

class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 

let goldenWordsYellow = UIColor(red: 247.00/255.0, green: 192.00/255.0, blue: 51.00/255.0, alpha: 0.5) 

// Map View outlet declaration 
@IBOutlet weak var mapView: MKMapView! 

// Hamburger button declaration 
@IBOutlet weak var menuButton:UIBarButtonItem! 

var locationManager: CLLocationManager? 

/* Really ugly code where I declare all of my static data */ 
let coordinatesARC = IssueLocation(locationName: "ARC", coordinate: CLLocationCoordinate2D(latitude: -76.49416565895079, longitude: 44.22928743712073)) 
let coordinatesJDUC = IssueLocation(locationName: "JDUC", coordinate: CLLocationCoordinate2D(latitude: -76.49507761001587, longitude: 44.22838027067406)) 
let coordinatesStaufferLibrary = IssueLocation(locationName: "Stauffer Library", coordinate: CLLocationCoordinate2D(latitude: -76.49615049362183, longitude: 44.228418710213944)) 
let coordinatesWalterLightHall = IssueLocation(locationName: "Walter Light Hall", coordinate: CLLocationCoordinate2D(latitude: -76.49166584014893, longitude: 44.22794205814507)) 
let coordinatesDupuisHall = IssueLocation(locationName: "Dupuis Hall", coordinate: CLLocationCoordinate2D(latitude: -76.4927065372467, longitude: 44.22867241054762)) 
let coordinatesHumphreyHall = IssueLocation(locationName: "Humphrey Hall", coordinate: CLLocationCoordinate2D(latitude: -76.49212718009949, longitude: 44.22688879714365)) 
let coordinatesBiosciencesComplex = IssueLocation(locationName: "Biosciences Complex", coordinate: CLLocationCoordinate2D(latitude: -76.49117231369019, longitude: 44.226327562781904)) 
let coordinatesBMH = IssueLocation(locationName: "Beamish-Munro Hall", coordinate: CLLocationCoordinate2D(latitude: -76.49271726608276, longitude: 44.228195760533175)) 
let coordinatesBotterellHall = IssueLocation(locationName: "Botterell Hall", coordinate: CLLocationCoordinate2D(latitude: -76.49160146713257, longitude: 44.22447468258034)) 
let coordinatesEtheringtonHall = IssueLocation(locationName: "Etherington Hall", coordinate: CLLocationCoordinate2D(latitude: -76.49390816688538, longitude: 44.224282471751785)) 
let coordinatesJefferyHall = IssueLocation(locationName: "Jeffery Hall", coordinate: CLLocationCoordinate2D(latitude: -76.49605393409729, longitude: 44.22590855555731)) 
let coordinatesEllisHall = IssueLocation(locationName: "Ellis Hall", coordinate: CLLocationCoordinate2D(latitude: -76.49602174758911, longitude: 44.22636984774898)) 
let coordinatesMackintoshCorryHall = IssueLocation(locationName: "Mackintosh-Corry Hall", coordinate: CLLocationCoordinate2D(latitude: -76.49697124958038, longitude: 44.22677731951135)) 
let coordinatesChernoffHall = IssueLocation(locationName: "Chernoff Hall", coordinate: CLLocationCoordinate2D(latitude: -76.49884343147278, longitude: 44.22436704459368)) 
let coordinatesLeggetHall = IssueLocation(locationName: "Legget Hall", coordinate: CLLocationCoordinate2D(latitude: -76.49749159812927, longitude: 44.22362126170883)) 
let coordinatesLeonardHall = IssueLocation(locationName: "Leonard Hall", coordinate: CLLocationCoordinate2D(latitude: -76.50065660476685, longitude: 44.22429016019697)) 
let coordinatesVictoriaHall = IssueLocation(locationName: "Victoria Hall", coordinate: CLLocationCoordinate2D(latitude: -76.49863958358765, longitude: 44.22550492192426)) 
let coordinatesStirlingHall = IssueLocation(locationName: "Stirling Hall", coordinate: CLLocationCoordinate2D(latitude: -76.49767398834229, longitude: 44.22463613919133)) 
let coordinatesWestCampus = IssueLocation(locationName: "West Campus", coordinate: CLLocationCoordinate2D(latitude: -76.51471138000487, longitude: 44.22438242146097)) 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Hamburger button configuration 
if self.revealViewController() != nil { 
    menuButton.target = self.revealViewController() 
    menuButton.action = "revealToggle:" 
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) 
    } 

    // Setting the Map type to standard 
    mapView.mapType = MKMapType.Standard 

    // Configuring locationManager and mapView delegate 
    locationManager = CLLocationManager() 
    mapView.delegate = self 

    // Set the center of campus as the first location, before we show the actual user location 
    let initialLocation = CLLocationCoordinate2D(latitude: 44.226181, longitude: -76.495614) 
    let latitudeDelta:CLLocationDegrees = 0.015 
    let longitudeDelta:CLLocationDegrees = 0.015 
    let span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta) 

    let coordinateRegion = MKCoordinateRegionMake(initialLocation, span) 

    mapView.setRegion(coordinateRegion, animated: true) 

    let ARCAnnotation = MKPointAnnotation() 
    ARCAnnotation.title = coordinatesARC.locationName 
    ARCAnnotation.subtitle = coordinatesARC.locationName 
    ARCAnnotation.coordinate = coordinatesARC.coordinate 

    mapView.addAnnotation(ARCAnnotation) 

    // Adding all annotations to the map view 
    mapView.addAnnotation(coordinatesARC) 
    mapView.addAnnotation(coordinatesJDUC) 
    mapView.addAnnotation(coordinatesStaufferLibrary) 
    mapView.addAnnotation(coordinatesWalterLightHall) 
    mapView.addAnnotation(coordinatesDupuisHall) 
    mapView.addAnnotation(coordinatesHumphreyHall) 
    mapView.addAnnotation(coordinatesBiosciencesComplex) 
    mapView.addAnnotation(coordinatesBMH) 
    mapView.addAnnotation(coordinatesBotterellHall) 
    mapView.addAnnotation(coordinatesEtheringtonHall) 
    mapView.addAnnotation(coordinatesJefferyHall) 
    mapView.addAnnotation(coordinatesEllisHall) 
    mapView.addAnnotation(coordinatesMackintoshCorryHall) 
    mapView.addAnnotation(coordinatesChernoffHall) 
    mapView.addAnnotation(coordinatesLeggetHall) 
    mapView.addAnnotation(coordinatesLeonardHall) 
    mapView.addAnnotation(coordinatesVictoriaHall) 
    mapView.addAnnotation(coordinatesStirlingHall) 
    mapView.addAnnotation(coordinatesWestCampus) 

mapView.showsUserLocation = true 
    locationManager?.startUpdatingLocation() 

} 


func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) { 
    mapView.centerCoordinate = CLLocationCoordinate2D(latitude: 44.226181, longitude: -76.495614) 
} 

func checkLocationAuthorizationStatus() { 

    if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse { 
     mapView.showsUserLocation = true 
    } else { 
     locationManager?.requestWhenInUseAuthorization() 
     } 
    } 

override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated) 
    checkLocationAuthorizationStatus() 
    } 

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

這裏是我的地圖視圖控制器擴展:

import Foundation 
import MapKit 

extension MapViewController { 



func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
    if let annotation = annotation as? IssueLocation { 
     let identifier = "pin" 
     var view: MKPinAnnotationView 
     if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) as? MKPinAnnotationView { 
      dequeuedView.annotation = annotation // NOTE: 2 options here for the "annotation" at the end of this line. I choose the first one but am in doubt. 
      view = dequeuedView 
     } else { 
      view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) // NOTE: another unsure choice for the "annotation" right before the comma 
      view.canShowCallout = true 
      view.calloutOffset = CGPoint(x: -5, y: 5) 
      view.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIView 
     } 

     return view 
    } 
    return nil 
} 

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
    let location = view.annotation as! IssueLocation 
    let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeWalking] 
    location.mapItem().openInMapsWithLaunchOptions(launchOptions) 
} 

} 

這裏是我的課:

import Foundation 
import MapKit 
import AddressBook 
import Contacts 

class IssueLocation: NSObject, MKAnnotation { 

let locationName: String 
let coordinate: CLLocationCoordinate2D 

init(locationName: String, coordinate: CLLocationCoordinate2D) { 

    self.locationName = locationName 
    self.coordinate = coordinate 

    super.init() 

} 

var subtitle: String? { 
    return locationName 
} 

var title: String? { 
    return locationName 
} 

func mapItem() -> MKMapItem { 

    let addressDictionary = [String(CNPostalAddressStreetKey): locationName] 

    let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDictionary) 

    let mapItem = MKMapItem(placemark: placemark) 
    mapItem.name = locationName 

    return mapItem 

回答

1

這個問題與你的座標。

您的代碼:

let coordinatesARC = IssueLocation(locationName: "ARC", coordinate: CLLocationCoordinate2D(latitude: -76.49416565895079, longitude: 44.22928743712073)) 
let coordinatesJDUC = IssueLocation(locationName: "JDUC", coordinate: CLLocationCoordinate2D(latitude: -76.49507761001587, longitude: 44.22838027067406)) 

更正代碼:

let coordinatesARC = IssueLocation(locationName: "ARC", coordinate: CLLocationCoordinate2D(latitude: 44.22928743712073, longitude: -76.49416565895079)) 
let coordinatesJDUC = IssueLocation(locationName: "JDUC", coordinate: CLLocationCoordinate2D(latitude: 44.22838027067406, longitude: -76.49507761001587)) 
+1

我最終能夠解決它(在相同的方式,固定的話),但由於反正。當我意識到我的座標指向南極洲時,我想起了一些事情:P –

+0

沒問題。感謝您的投票並將其標記爲答案。也就像你的代碼的功能一樣,在所有其他編碼方面都做得很好。 – MwcsMac