2016-09-20 51 views
0

我在我的项目开始时在用户位置上工作过mapview,但现在它不会在启动时放大用户。任何想法如何解决这个问题?代码我使用:Swift User Location Issue

在类

class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate 

    var Locationmanager = CLLocationManager() 

在viewDidLoad中

self.Locationmanager.delegate = self 
    self.Locationmanager.desiredAccuracy = kCLLocationAccuracyBest 
    self.Locationmanager.requestWhenInUseAuthorization() 
    self.Locationmanager.startUpdatingLocation() 
    self.Mapview.showsUserLocation = true 
    Mapview.setCenterCoordinate(Mapview.userLocation.coordinate, animated: true) 

在功能

 func locationManager(manager: CLLocationManager, didUpdateLocations locations:[MKUserLocation]) { 

     let latitude = Locationmanager.location?.coordinate.latitude 
     let longitude = Locationmanager.location?.coordinate.longitude 
     let center = CLLocationCoordinate2D (latitude: latitude!, longitude: longitude!) 
     let region = MKCoordinateRegion (center: center, span: MKCoordinateSpan (latitudeDelta: 0.02, longitudeDelta: 0.02)) 
     self.Mapview.setRegion(region, animated: true) 
     self.Locationmanager.stopUpdatingLocation() 

而在info.plist中

 NSLocationWhenInUseUsageDescription 

回答

0

如果您的目标是使地图视图显示用户的位置,则无需任何指示即可告知位置管理员执行任何操作。摆脱所有的代码 - 它实际上在这里对你造成伤害。只需将地图视图的userTrackingMode设置为.follow并退后即可。

+0

嗯,我需要问的用户位置的权限..将尝试并删除,看看发生了什么 – Adam

+0

仍然没有得到在启动用户位置的变焦.. – Adam

+0

不能解决它..任何其他用于查找用户位置的代码? – Adam