2016-02-29 1 views
1

Salut, j'ai le code ci-dessous qui est de trouver l'emplacement de l'utilisateur le rayon à 1 mile:Comment centrer une carte sur l'utilisateur Localisation IOS 9 SWIFT

let initialLocation = CLLocation(latitude: 51.5001524, longitude: -0.1262362) 
let regionRadius: CLLocationDistance = 1000 
var locationManager: CLLocationManager! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    func centerMapOnLocation(location:CLLocation) { 
     let coordianteRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 
      regionRadius * 2.0, regionRadius * 2.0) 
     mapView.setRegion(coordianteRegion, animated: true) 
    } 
    // Do any additional setup after loading the view, typically from a nib. 
     centerMapOnLocation(initialLocation) 

     } 

This is perfect as gets a decent radius for user location, however i want to know, how do you center map on user location? 

j'ai essayé beaucoup de méthodes, mais je ne semblent être en mesure d'obtenir la carte au centre sur l'emplacement de l'utilisateur.

Toute aide serait grandement appréciée!

Merci

Répondre

7

MKMapView a construit en solution en utilisant la propriété userTrackingMode.

mapView.userTrackingMode = .Follow 
+0

merci a fonctionné parfaitement !! – Yondaime14

4

Je pense que cela pourrait vous aider à

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

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)   
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)) 

    self.map.setRegion(region, animated: true) 
}