gpt4 book ai didi

swift - GeoFire + Swift+ Firebase : Save current location

转载 作者:行者123 更新时间:2023-11-30 11:59:27 30 4
gpt4 key购买 nike

这正在工作:

geoFire.setLocation(CLLocation(latitude: 37.7853889, longitude: -122.4056973), 
forKey: "firebase-hq") { (error) in
if (error != nil) {
println("An error occured: \(error)")
} else {
println("Saved location successfully!")
}
}

但是如何将当前位置坐标发送到我的 Firebase?像这样?但 currentLocation 不起作用。

geoFire!.setLocation(currentLocation, forKey: "firebase-hq") { (error) in
if (error != nil) {
print("An error occured: \(error)")
} else {
print("Saved location successfully!")
}

最佳答案

除非完全有必要,否则我建议不要使用 GeoFire,而应使用苹果的内置 API。首先,打开您的 info.plist 文件作为源代码并将以下内容添加到文件中

<key>NSLocationAlwaysUsageDescription</key>
<string>Will you allow this app to always know your location?
</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Do you allow this app to know your current location?</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Will you allow this app to always know your current location?
</string>

然后将此代码添加到您的新 View Controller 中:

import UIKit
import CoreLocation


class SearchViewController: UIViewController, CLLocationManagerDelegate {

var locationManager:CLLocationManager!



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

determineMyCurrentLocation()
}


func determineMyCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
//locationManager.startUpdatingHeading()
}
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation

// Call stopUpdatingLocation() to stop listening for location updates,
// other wise this function will be called every time when user location changes.

// manager.stopUpdatingLocation()

print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")



}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
}

这会将您的纬度和经度打印为模拟器中的默认当前位置(位于旧金山的某个位置)。要更改此设置,请进入模拟器并进入调试菜单,然后单击位置、当前位置并输入您首选的新值。

关于swift - GeoFire + Swift+ Firebase : Save current location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47438252/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com