我正在学习如何使用新的Swift语言(只有Swift,没有Objective-C)。要做到这一点,我想用地图(MKMapView
)做一个简单的视图。我想查找并更新用户的位置(例如在Apple Map应用程序中)。
我试过了,但没有发生任何事情:
import MapKit
import CoreLocation
class MapView : UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
if (CLLocationManager.locationServicesEnabled())
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
}
}
你能帮我吗?
答案 0 :(得分:59)
当位置管理员检索当前位置时,您必须覆盖CLLocationManager.didUpdateLocations
(CLLocationManagerDelegate的一部分)以获得通知:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last{
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)
}
}
注意:如果您的目标是iOS 8或更高版本,则必须在Info.plist中包含NSLocationAlwaysUsageDescription
或NSLocationWhenInUseUsageDescription
密钥才能使位置服务正常运行。
答案 1 :(得分:20)
对于swift 3和XCode 8,我找到了这个答案:
首先,您需要将隐私设置为info.plist。将字符串 NSLocationWhenInUseUsageDescription 与您的描述一起插入您想要获取用户位置的原因。例如,设置字符串"对于应用程序中的地图"。
其次,请使用此代码示例
@IBOutlet weak var mapView: MKMapView!
private var locationManager: CLLocationManager!
private var currentLocation: CLLocation?
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
// Check for Location Services
if CLLocationManager.locationServicesEnabled() {
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
}
// MARK - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
defer { currentLocation = locations.last }
if currentLocation == nil {
// Zoom to user location
if let userLocation = locations.last {
let viewRegion = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 2000, 2000)
mapView.setRegion(viewRegion, animated: false)
}
}
}
第三,在mapView的故事板中设置用户位置标志。
答案 2 :(得分:19)
100%正常工作,轻松完成步骤并经过测试
导入库:
import MapKit
import CoreLocation
设置代理:
CLLocationManagerDelegate,MKMapViewDelegate
采取变量:
let locationManager = CLLocationManager()
在viewDidLoad()上编写此代码:
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
mapView.delegate = self
mapView.mapType = .standard
mapView.isZoomEnabled = true
mapView.isScrollEnabled = true
if let coor = mapView.userLocation.location?.coordinate{
mapView.setCenter(coor, animated: true)
}
为位置编写委托方法:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
mapView.mapType = MKMapType.standard
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegion(center: locValue, span: span)
mapView.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = locValue
annotation.title = "Javed Multani"
annotation.subtitle = "current location"
mapView.addAnnotation(annotation)
//centerMap(locValue)
}
不要忘记在 info.plist
中设置权限<key>NSLocationWhenInUseUsageDescription</key>
<string>This application requires location services to work</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This application requires location services to work</string>
它看起来像:
答案 3 :(得分:7)
MyLocation 是一个Swift iOS演示。
您可以将此演示用于以下内容:
显示当前位置。
选择其他位置:在这种情况下,请停止跟踪位置。
触摸时将推针添加到MKMapView(iOS)。
答案 4 :(得分:3)
对于Swift 2,您应该将其更改为以下内容:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
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)
}
答案 5 :(得分:2)
您只需要设置MKMapView的userTrackingMode。如果您只想显示和跟踪用户位置并实现与Apple Maps应用程序相同的行为,则无需编写其他代码。
mapView.userTrackingMode = .follow
在https://developer.apple.com/documentation/mapkit/mkmapview/1616208-usertrackingmode上查看更多内容。
答案 6 :(得分:1)
您必须覆盖CLLocationManager.didUpdateLocations
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation
locationManager.stopUpdatingLocation()
let location = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
let span = MKCoordinateSpanMake(0.5, 0.5)
let region = MKCoordinateRegion (center: location,span: span)
mapView.setRegion(region, animated: true)
}
您还必须将NSLocationWhenInUseUsageDescription
和NSLocationAlwaysUsageDescription
添加到您的plist设置Result
作为值
答案 7 :(得分:1)
在Swift 4中,我使用了上面定义的locationManager委托函数。
func locationManager(manager: CLLocationManager!,
didUpdateLocations locations: [AnyObject]!) {
..但需要将其更改为..
func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
这来自...... https://github.com/lotfyahmed/MyLocation/blob/master/MyLocation/ViewController.swift - 谢谢!
答案 8 :(得分:1)
Swift 5.1
获取当前位置并在MKMapView上设置
导入库:
import MapKit
import CoreLocation
设置代表:
CLLocationManagerDelegate , MKMapViewDelegate
声明变量:
let locationManager = CLLocationManager()
将此代码写在viewDidLoad()上:
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
mapView.delegate = self
mapView.mapType = .standard
mapView.isZoomEnabled = true
mapView.isScrollEnabled = true
if let coor = mapView.userLocation.location?.coordinate{
mapView.setCenter(coor, animated: true)
}
为位置写委托方法:
func locationManager(_ manager: CLLocationManager, didUpdateLocations
locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
mapView.mapType = MKMapType.standard
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion(center: locValue, span: span)
mapView.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = locValue
annotation.title = "You are Here"
mapView.addAnnotation(annotation)
}
在info.plist中设置权限*
<key>NSLocationWhenInUseUsageDescription</key>
<string>This application requires location services to work</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This application requires location services to work</string>
答案 9 :(得分:0)
您好有时出于某些奇怪的原因,无法在代码中设置showsUserLocation。
因此,请尝试以下组合。
在viewDidLoad()
self.mapView.showsUserLocation = true
转到Xcode中的情节提要,在右侧面板的属性检查器上,选中用户位置复选框,与所附图像类似,。运行您的应用,您应该可以看到用户位置
答案 10 :(得分:0)
mapView.showsUserLocation = true
创建 UIButton 并添加这个动作
@IBAction func showCurrentLocation(_ sender: Any) {
let coordinate = mapView.userLocation.coordinate
let center = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region, animated: true)
}