您好我有下面的代码,它找到半径在1英里范围内的用户位置:
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?
我尝试了很多方法,但似乎无法让地图以用户所在位置为中心。
非常感谢任何帮助!
由于
答案 0 :(得分:14)
MKMapView
使用userTrackingMode
属性构建了内置解决方案。
mapView.userTrackingMode = .Follow
答案 1 :(得分:4)
我认为这可能会对你有所帮助
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)
}