gpt4 book ai didi

swift - 如何使用Cloud Firestore中的geopoint设置tableview以将商店放置在离我当前位置最近的位置?

转载 作者:行者123 更新时间:2023-12-02 22:56:22 26 4
gpt4 key购买 nike

我要尝试的是使用Cloud Firestore中的Geopoint按离当前位置最近的商店来组织我的单元

我已经查看了整个堆栈,但是找不到如何在ViewController中设置表格视图,以使用Cloud Firestore中的geopoint显示距当前位置最近的我的鲜花店

这就是我目前要设置的数据,以传递给VC,以便将数据从Firestore整理到距离我当前位置最近的商店

下面,我在Firestore中有一个集合的图像,还有一个关于我的应用程序设置方式的ViewController的图像

enter image description here
enter image description here

import Foundation
import UIKit

class Store {
var id: String
var storeName: String
var imageUrl: String
var location: ??

init(id: String,
storeName: String,
imageUrl: String,
location:??) { //

self.id = id
self.storeName = storeName
self.imageUrl = imageUrl
self.location = location //
}

convenience init(dictionary: [String : Any]) {
let id = dictionary["id"] as? String ?? ""
let storeName = dictionary["storeName"] as? String ?? ""
let imageUrl = dictionary["imageUrl"] as? String ?? ""
let location = dictionary["location"] as? String ?? "" //

self.init(id: id,
storeName: storeName,
imageUrl: imageUrl,
location: location) //
}

}




import UIKit
import CoreLocation
import Firebase
import FirebaseFirestore

class ViewController: UIViewController, CLLocationManagerDelegate {

var locationManager: CLLocationManager?

@IBOutlet weak var tableView: UITableView!

var stores: [Store] = []

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

override func viewDidLoad() {
super.viewDidLoad()

locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestWhenInUseAuthorization()

tableView.dataSource = self
tableView.delegate = self

fetchStores { (stores) in
self.stores = stores
self.tableView.reloadData()
}

}

func fetchStores(_ completion: @escaping ([Store]) -> Void) {
let ref = Firestore.firestore().collection("storeName")
ref.addSnapshotListener { (snapshot, error) in
guard error == nil, let snapshot = snapshot, !snapshot.isEmpty else {
return
}
completion(snapshot.documents.compactMap( {Store(dictionary: $0.data())} ))
}
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self){
if CLLocationManager.isRangingAvailable() {
// do Stuff
}
}
}
}
}


enter image description here

最佳答案

我相信这个答案应该有用,尽管我可能已经错过了一些东西。

您要做的是将location属性设置为Store类型的CLLocationCoordinate2D。这需要导入CoreLocation。您还希望添加一个名为distanceFromUser的属性,该属性使用CLLocation.distance(from:)方法来查找用户当前位置与商店位置之间的距离(以米为单位):

import Foundation
import UIKit
import CoreLocation
import Firebase
import FirebaseFirestore

class Store {
var id: String
var storeName: String
var imageUrl: String
var location: CLLocationCoordinate2D
var distanceFromUser: Double

init(id: String,
storeName: String,
imageUrl: String,
location: CLLocationCoordinate2D) {

self.id = id
self.storeName = storeName
self.imageUrl = imageUrl
self.location = location
self.distanceFromUser = (CLLocationManager().location?.distance(from: CLLocation(latitude: location.latitude, longitude: location.longitude)))!
}

convenience init(dictionary: [String : Any]) {
let id = dictionary["id"] as? String ?? ""
let storeName = dictionary["storeName"] as? String ?? ""
let imageUrl = dictionary["imageUrl"] as? String ?? ""

//We now have to convert Firestore's "location" property from a GeoPoint to a CLLocationCoordinate2d
let geoPoint = dictionary["location"] as! GeoPoint
let latitude = geoPoint.latitude
let longitude = geoPoint.longitude

let location = CLLocationCoordinate2D(latitude: latitude!, longitude: longitude!)

self.init(id: id,
storeName: storeName,
imageUrl: imageUrl,
location: location)
}
}


然后,您需要在 distanceFromUserViewController中按 viewDidLoad对商店进行排序:

import UIKit
import CoreLocation
import Firebase
import FirebaseFirestore

class ViewController: UIViewController, CLLocationManagerDelegate {

...

override func viewDidLoad() {
super.viewDidLoad()

locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestWhenInUseAuthorization()

tableView.dataSource = self
tableView.delegate = self

fetchStores { (stores) in
self.stores = stores.sorted(by: { $0.distanceFromUser < $1.distanceFromUser })
self.tableView.reloadData()
}

}

...

}

关于swift - 如何使用Cloud Firestore中的geopoint设置tableview以将商店放置在离我当前位置最近的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58611892/

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