如何快速将地图中心放在用户的位置? [英] How I can center the map on user's location in swift?
问题描述
我正在编写一个应用程序,并且具有一个嵌入式地图视图来显示用户的位置.到目前为止,这是我的代码:
I'm writing an app and I have an embedded mapview to show user's his location. This is my code so far:
class YourCurrentLocation: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
let regionRadius: CLLocationDistance = 1000
func checkLocationAuthorizationStatus() {
if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
mapView.showsUserLocation = true
centerMapOnLocation(locationManager.location!, map: mapView, radius: regionRadius)
} else {
locationManager.requestAlwaysAuthorization() //requestWhenInUseAuthorization()
}
}
func centerMapOnLocation(location: CLLocation, map: MKMapView, radius: CLLocationDistance) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
radius * 2.0, radius * 2.0)
map.setRegion(coordinateRegion, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
// mapView.delegate = self
if CLLocationManager.locationServicesEnabled()
{
//locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
print("location enabled")
checkLocationAuthorizationStatus()
}
else
{
print("Location service disabled");
}
// Do any additional setup after loading the view.
}
}
我还将两个条目添加到我的plist中:
I also added the two entries to my plist:
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
并且在我的xcode中,我已经设置为模拟英国伦敦的GPS数据.
and also in my xcode I have set to emulate the GPS data on london, UK.
当我运行该应用程序时-我看到了地图,但未标记伦敦.我在做什么错了?
When I run the app - I see the map, but london is not marked. What am I doing wrong?
顺便说一句,我不得不注释掉这一行:
Btw, I had to comment out this line:
//mapView.delegate = self
,否则出现错误: in ,我不确定这是否是问题的一部分. and I'm not sure if that's a part of the problem here. 当我显示给用户地图以及在该地图上标有他的位置的点时,我想实现这种效果.你能帮我吗? I want to achieve the effect when I display to the user map and a point marked on that map with his location. Can you help me with that? 您的代码存在的问题是,当用户授予位置权限时,您试图将地图指向用户的位置,而不是在等待 The problem with your code is that you're trying to point the map to the user's location when the user gives location permission, you're not waiting for the 这篇关于如何快速将地图中心放在用户的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!viewDidLoad()
, otherwise I had the error: Cannot assign value of type YourCurrentLocation to type MKMapViewDelegate
推荐答案
CoreLocation
为您提供实际的用户位置.
您需要使用CLLocationManagerDelegate
方法locationManager(_:didUpdateLocations:)
来通知您何时获得用户的实际位置,并且有您可以将地图设置为指向用户的位置.CoreLocation
to give you the actual user location.
You need to use the CLLocationManagerDelegate
method locationManager(_:didUpdateLocations:)
to be notified of when you get the user's actual location, and there you can set the map to point to the user's location.class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet var mapView: MKMapView!
var locationManager: CLLocationManager?
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager!.delegate = self
if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
locationManager!.startUpdatingLocation()
} else {
locationManager!.requestWhenInUseAuthorization()
}
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .NotDetermined:
print("NotDetermined")
case .Restricted:
print("Restricted")
case .Denied:
print("Denied")
case .AuthorizedAlways:
print("AuthorizedAlways")
case .AuthorizedWhenInUse:
print("AuthorizedWhenInUse")
locationManager!.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.first!
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500)
mapView.setRegion(coordinateRegion, animated: true)
locationManager?.stopUpdatingLocation()
locationManager = nil
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Failed to initialize GPS: ", error.description)
}
}