2016-01-13 66 views
0

我正在開發一個簡單的應用程序,它將顯示一個地圖和一些標記。我也想要在這個應用程序中顯示用戶的當前位置。 這裏是我到目前爲止的代碼:縮放和當前位置不顯示在地圖swift

import UIKit 
import MapKit 
import CoreLocation 

class HomePageViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 

    @IBOutlet weak var templabelforlocationtesting: UILabel! 
    @IBOutlet weak var map: MKMapView! 
    let locationManager = CLLocationManager() 
    @IBOutlet weak var menuButton: UIBarButtonItem! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

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

     // Do any additional setup after loading the view. 
     self.locationManager.delegate = self 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     self.locationManager.requestAlwaysAuthorization() 
     self.locationManager.startUpdatingLocation() 
     self.map.showsUserLocation = true 

     //temporary nearby locations list 

     //temple tooth 
     let templeToothMarker = MKPointAnnotation() 
     let templeToothLocation = CLLocationCoordinate2D(latitude: 7.294715, longitude: 80.639858) 
     templeToothMarker.coordinate = templeToothLocation 
     templeToothMarker.title = "Kandy Dalada Maligawa" 
     templeToothMarker.subtitle = "Historic Religious place" 

     //botanical gardens 

     let botanicalGardenMarker = MKPointAnnotation() 
     let botanicalGardenLocation = CLLocationCoordinate2D(latitude: 7.273346, longitude: 80.595140) 
     botanicalGardenMarker.coordinate = botanicalGardenLocation 
     botanicalGardenMarker.title = "Royal Botanical Gardens" 
     botanicalGardenMarker.subtitle = "Botanical Garden in Kandy" 

     //load markers to map 
     map.addAnnotations([templeToothMarker, botanicalGardenMarker]) 

    } 

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     let location = locations.last! as CLLocation 

     let centerLocation = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) 
     templabelforlocationtesting.text = "Location: " + String(location.coordinate.latitude) + " , " + String(location.coordinate.longitude) 

     let mapSpan = MKCoordinateSpanMake(0.06, 0.06) 

     let mapRegion = MKCoordinateRegion(center: centerLocation, span: mapSpan) 

     self.map.setRegion(mapRegion, animated: true) 
     self.locationManager.stopUpdatingLocation() 
    } 


} 

的地圖是否工作正常,標記出,但下面的問題依然存在。

  1. 我不能得到的地圖放大到更高的水平(在let mapSpan = MKCoordinateSpanMake(0.06, 0.06)設置的級別)

  2. 的用戶的當前位置,應獲得並設定爲地圖的中心和應也顯示爲帶有註釋的當前位置。要檢查位置是否已加載,我也設置了標籤,但這不起作用。

我在做什麼錯在這裏?

+0

如果你在** let location = locations.last!處放置一個斷點,作爲CLLocation **你會得到什麼值? – MwcsMac

回答

1

該代碼結構永遠是錯的:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    // ... 
    self.locationManager.stopUpdatingLocation() 
} 

的問題是,你會得到一個可用的位置之前,它需要許多調用locationManager:didUpdateLocations:。但是你在第一次呼叫後無條件停止,這幾乎肯定是無用的。如果你是告訴它self.map.showsUserLocation = true。如果地圖的showsUserLocationtrue,地圖將設置本身以顯示正確的區域。事實上,如果您只想將地圖設置爲用戶的位置,則不應該使用位置管理器更新位置。讓地圖視圖完成所有工作。

相關問題