位置标记未显示 Swift 4
Location Marker Not Displaying Swift 4
我正在尝试显示用户的当前位置并将区域设置为其当前位置。
这是我的代码。
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
let userLocation = locations.last
let viewRegion = MKCoordinateRegionMakeWithDistance((userLocation?.coordinate)!, 600, 600)
self.mapView.setRegion(viewRegion, animated: true)
}
发生了什么:
- 地图加载
- 地图区域设置为当前位置
- 地图区域会不断更新到当前位置
- 当前位置被打印到控制台
什么不起作用:
- "Blue" 当前位置指示符未出现。
如果您知道我需要做什么才能显示 "Blue" 当前位置指示器,将不胜感激。
您需要设置
mapView.showsUserLocation = true
在viewDidLoad
我正在尝试显示用户的当前位置并将区域设置为其当前位置。
这是我的代码。
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
let userLocation = locations.last
let viewRegion = MKCoordinateRegionMakeWithDistance((userLocation?.coordinate)!, 600, 600)
self.mapView.setRegion(viewRegion, animated: true)
}
发生了什么:
- 地图加载
- 地图区域设置为当前位置
- 地图区域会不断更新到当前位置
- 当前位置被打印到控制台
什么不起作用:
- "Blue" 当前位置指示符未出现。
如果您知道我需要做什么才能显示 "Blue" 当前位置指示器,将不胜感激。
您需要设置
mapView.showsUserLocation = true
在viewDidLoad