2016-01-13 78 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,地图将设置本身以显示正确的区域。事实上,如果您只想将地图设置为用户的位置,则不应该使用位置管理器更新位置。让地图视图完成所有工作。

相关问题