如何存储用户的当前位置,并在地图上显示该位置?
我可以在地图上显示预定义的坐标,只是不知道如何从设备接收信息。
我也知道我必须添加一些项目到Plist。我该怎么做呢?
如何存储用户的当前位置,并在地图上显示该位置?
我可以在地图上显示预定义的坐标,只是不知道如何从设备接收信息。
我也知道我必须添加一些项目到Plist。我该怎么做呢?
当前回答
斯威夫特3.0
如果你不想在地图中显示用户位置,而只是想将其存储在firebase或其他地方,那么请遵循以下步骤,
import MapKit
import CoreLocation
现在在你的VC上使用CLLocationManagerDelegate,你必须覆盖下面显示的最后三个方法。您可以看到requestLocation()方法如何使用这些方法获取当前用户位置。
class MyVc: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
isAuthorizedtoGetUserLocation()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
}
}
//if we have no permission to access user location, then ask user for permission.
func isAuthorizedtoGetUserLocation() {
if CLLocationManager.authorizationStatus() != .authorizedWhenInUse {
locationManager.requestWhenInUseAuthorization()
}
}
//this method will be called each time when a user change his location access preference.
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
print("User allowed us to access location")
//do whatever init activities here.
}
}
//this method is called by the framework on locationManager.requestLocation();
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Did location updates is called")
//store the user location here to firebase or somewhere
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Did location updates is called but failed getting location \(error)")
}
}
现在,一旦用户登录到您的应用程序,您就可以编写下面的调用。当requestLocation()被调用时,它将进一步调用上面的didUpdateLocations,您可以将位置存储到Firebase或其他任何地方。
if CLLocationManager.locationServicesEnabled() {
locationManager.requestLocation();
}
如果你正在使用GeoFire,那么在上面的didUpdateLocations方法中,你可以像下面那样存储位置
geoFire?.setLocation(locations.first, forKey: uid) where uid is the user id who logged in to the app. I think you will know how to get UID based on your app sign in implementation.
最后但并非最不重要的,去你的信息。plist并启用“Privacy -Location when in Use Usage Description.”
当您使用模拟器测试时,它总是给您一个自定义位置,您在模拟器中配置->调试->位置。
其他回答
用法:
在类中定义字段
let getLocation = GetLocation()
通过简单的代码在类的函数中使用:
getLocation.run {
if let location = $0 {
print("location = \(location.coordinate.latitude) \(location.coordinate.longitude)")
} else {
print("Get Location failed \(getLocation.didFailWithError)")
}
}
类:
import CoreLocation
public class GetLocation: NSObject, CLLocationManagerDelegate {
let manager = CLLocationManager()
var locationCallback: ((CLLocation?) -> Void)!
var locationServicesEnabled = false
var didFailWithError: Error?
public func run(callback: @escaping (CLLocation?) -> Void) {
locationCallback = callback
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
manager.requestWhenInUseAuthorization()
locationServicesEnabled = CLLocationManager.locationServicesEnabled()
if locationServicesEnabled { manager.startUpdatingLocation() }
else { locationCallback(nil) }
}
public func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
locationCallback(locations.last!)
manager.stopUpdatingLocation()
}
public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
didFailWithError = error
locationCallback(nil)
manager.stopUpdatingLocation()
}
deinit {
manager.stopUpdatingLocation()
}
}
不要忘记在info.plist中添加“NSLocationWhenInUseUsageDescription”。
你应该做这些步骤:
add CoreLocation.framework to BuildPhases -> Link Binary With Libraries (no longer necessary as of XCode 7.2.1) import CoreLocation to your class - most likely ViewController.swift add CLLocationManagerDelegate to your class declaration add NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription to plist init location manager: locationManager = CLLocationManager() locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() get User Location By: func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let locValue:CLLocationCoordinate2D = manager.location!.coordinate print("locations = \(locValue.latitude) \(locValue.longitude)") }
// its with strongboard
@IBOutlet weak var mapView: MKMapView!
//12.9767415,77.6903967 - exact location latitude n longitude location
let cooridinate = CLLocationCoordinate2D(latitude: 12.9767415 , longitude: 77.6903967)
let spanDegree = MKCoordinateSpan(latitudeDelta: 0.2,longitudeDelta: 0.2)
let region = MKCoordinateRegion(center: cooridinate , span: spanDegree)
mapView.setRegion(region, animated: true)
斯威夫特3.0
如果你不想在地图中显示用户位置,而只是想将其存储在firebase或其他地方,那么请遵循以下步骤,
import MapKit
import CoreLocation
现在在你的VC上使用CLLocationManagerDelegate,你必须覆盖下面显示的最后三个方法。您可以看到requestLocation()方法如何使用这些方法获取当前用户位置。
class MyVc: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
isAuthorizedtoGetUserLocation()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
}
}
//if we have no permission to access user location, then ask user for permission.
func isAuthorizedtoGetUserLocation() {
if CLLocationManager.authorizationStatus() != .authorizedWhenInUse {
locationManager.requestWhenInUseAuthorization()
}
}
//this method will be called each time when a user change his location access preference.
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
print("User allowed us to access location")
//do whatever init activities here.
}
}
//this method is called by the framework on locationManager.requestLocation();
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Did location updates is called")
//store the user location here to firebase or somewhere
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Did location updates is called but failed getting location \(error)")
}
}
现在,一旦用户登录到您的应用程序,您就可以编写下面的调用。当requestLocation()被调用时,它将进一步调用上面的didUpdateLocations,您可以将位置存储到Firebase或其他任何地方。
if CLLocationManager.locationServicesEnabled() {
locationManager.requestLocation();
}
如果你正在使用GeoFire,那么在上面的didUpdateLocations方法中,你可以像下面那样存储位置
geoFire?.setLocation(locations.first, forKey: uid) where uid is the user id who logged in to the app. I think you will know how to get UID based on your app sign in implementation.
最后但并非最不重要的,去你的信息。plist并启用“Privacy -Location when in Use Usage Description.”
当您使用模拟器测试时,它总是给您一个自定义位置,您在模拟器中配置->调试->位置。
首先在项目中添加两个框架
1:地
2:共重定位(从XCode 7.2.1开始不再需要)
在类中定义
var manager:CLLocationManager!
var myLocations: [CLLocation] = []
然后在viewDidLoad方法中编写此代码
manager = CLLocationManager()
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestAlwaysAuthorization()
manager.startUpdatingLocation()
//Setup our Map View
mapobj.showsUserLocation = true
不要忘记在plist文件中添加这两个值
1: NSLocationWhenInUseUsageDescription
2: NSLocationAlwaysUsageDescription