将地图置于用户位置的中心未显示蓝点
center the map on user's location didn't show the blue dot
我有一个标签栏,一个是用来显示地图的。当我切换到这个标签栏时,它会请求权限并自动获取当前位置。
我写完代码,设置模拟位置为苹果,会在屏幕中央显示一个蓝点,但是当我选择自定义位置,指定一个位置时,蓝点不显示在屏幕中央地图的中心。
我需要拖动地图,会在中心位置附近约1~2公里处找到蓝点。我在真机上测试也有同样的问题
使用方法自动显示当前位置,但不显示蓝点
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager!.delegate = self
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse{
locationManager!.startUpdatingLocation()
locationManager!.desiredAccuracy = kCLLocationAccuracyBestForNavigation
}else{
locationManager!.requestWhenInUseAuthorization()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last!
let coordinateRegion = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 100, 100)
mapView.setRegion(mapView.regionThatFits(coordinateRegion), animated: true)
location = newLocation
locationManager?.stopUpdatingLocation()
locationManager = nil
}
我添加了一个按钮 - 当它没有显示蓝点时,点击按钮会将蓝点设置在地图的中心。这两种方法具有相同的功能,但它们产生不同的视图。为什么?
@IBAction func showUser() {
let region = MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 100, 100)
mapView.setRegion(mapView.regionThatFits(region), animated: true)
}
the screenshot
我只是使用此编码更新到地图视图中心的位置
请将此密钥添加到 info.plist 文件
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
map.delegate = self
map.showsUserLocation = true
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse{
locationManager.startUpdatingLocation()
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
}else{
locationManager.requestWhenInUseAuthorization()
}
}
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
mapView .setCenter(userLocation.coordinate, animated: true)
}
您可以从 HERE
下载演示
我有一个标签栏,一个是用来显示地图的。当我切换到这个标签栏时,它会请求权限并自动获取当前位置。
我写完代码,设置模拟位置为苹果,会在屏幕中央显示一个蓝点,但是当我选择自定义位置,指定一个位置时,蓝点不显示在屏幕中央地图的中心。
我需要拖动地图,会在中心位置附近约1~2公里处找到蓝点。我在真机上测试也有同样的问题
使用方法自动显示当前位置,但不显示蓝点
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager!.delegate = self
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse{
locationManager!.startUpdatingLocation()
locationManager!.desiredAccuracy = kCLLocationAccuracyBestForNavigation
}else{
locationManager!.requestWhenInUseAuthorization()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last!
let coordinateRegion = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 100, 100)
mapView.setRegion(mapView.regionThatFits(coordinateRegion), animated: true)
location = newLocation
locationManager?.stopUpdatingLocation()
locationManager = nil
}
我添加了一个按钮 - 当它没有显示蓝点时,点击按钮会将蓝点设置在地图的中心。这两种方法具有相同的功能,但它们产生不同的视图。为什么?
@IBAction func showUser() {
let region = MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 100, 100)
mapView.setRegion(mapView.regionThatFits(region), animated: true)
}
the screenshot
我只是使用此编码更新到地图视图中心的位置
请将此密钥添加到 info.plist 文件
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
map.delegate = self
map.showsUserLocation = true
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse{
locationManager.startUpdatingLocation()
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
}else{
locationManager.requestWhenInUseAuthorization()
}
}
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
mapView .setCenter(userLocation.coordinate, animated: true)
}
您可以从 HERE
下载演示