2016-01-23 63 views
1

當我直接使用故事板上的@IBOutlet var MKMapView時,此代碼非常完美。在UIView中加載MKMapView

現在我想用在故事板上使用@IBOutlet var UIView加載地圖,然後創建一個MKMapView,將其設置到這個代碼,但UIView ......

我的問題鹼液,試圖展示用戶位置並在地圖上放置一個別針。此代碼僅顯示通用地圖,不會放大放置的引腳或顯示用戶位置。

class SecondRow: UITableViewCell,MKMapViewDelegate, CLLocationManagerDelegate { 

    @IBOutlet var eventLocation: UILabel! 
    @IBOutlet var eventDistance: UILabel! 

    //old Linkage through storyboard when everything worked 
    //@IBOutlet var mapView: MKMapView! 

    var pins: [MKAnnotation] = [] 
    var locationManager = CLLocationManager() 

//linked through the storyboard 
    @IBOutlet var mView: UIView! 

//created and Initialized here in this class 
    @IBOutlet var mapView: MKMapView! 


    func ConstructCell(Event:AppEvent)->Void { 

     eventLocation.text = Event.getAddress(); 
     eventDistance.text = String(Event.getDistance()) + " miles"; 

     createMap(Event) 

    } 

我需要知道爲什麼這個代碼不顯示用戶的位置,而不是放置圖釘當我使用mapView = MKMapView()創建mapView

它只是顯示一張通用地圖放大一路,所以我知道我的UIViewMKMapView之間的鏈接正在工作。

func createMap(Event:AppEvent) 
    { 

     self.mapView = MKMapView() 
     self.mapView.delegate = self 

     self.locationManager.delegate = self; 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     self.locationManager.requestWhenInUseAuthorization() 

     self.mapView.showsUserLocation = true 
     self.locationManager.startUpdatingLocation() 

     let eventPin = MKPointAnnotation(); 
     pins.append(eventPin) 
     if pins.count > 1{ 
      mapView.removeAnnotations(pins) 
     } 

     Event.getGeoLocation() 
     let coord: CLLocationCoordinate2D =  CLLocationCoordinate2DMake(Event.getGeoLocation().latitude, Event.getGeoLocation().longitude) 
     eventPin.coordinate = coord 
     eventPin.title = Event.getTitle() 
     mapView.addAnnotation(eventPin) 

     let span:MKCoordinateSpan = MKCoordinateSpanMake(0.05,0.05) 
     let region:MKCoordinateRegion = MKCoordinateRegion(center: coord, span: span) 

     self.mapView.setRegion(region, animated: true) 

     self.mView.addSubview(self.mapView) 
    } 

//Destroy the map to free up memory 
    func viewWillDisappear(animated: Bool) { 
     self.mapView.showsUserLocation = false 
     self.mapView.delegate = nil 
     self.mapView.removeFromSuperview() 
     self.mapView = nil 

    } 
+0

你並不需要設置區域或致電'startUpdatingLocation'。一旦設置了'self.mapView.showsUserLocation = true',地圖視圖將只顯示用戶位置。 – matt

回答

0

你有沒有實現MKMapViewDelegate - (nullable MKAnnotationView *)viewForAnnotation:(id <MKAnnotation>)annotation;

+0

我相信如此。我在swift中編寫代碼,我相信你的答案在Objective C中。我爲我的課程添加了其他代碼,希望這有助於。 – GREENEgene