![](/img/trans.png)
[英]I want to get latitude and longitude from Cloud Firestore database to add markers on map Android Studio
[英]SWIFT Longitude and latitude from GoogleAutocomplete save in Firestore as nil
我正在嘗試將經度和緯度從 GoogleAutocomlete 保存到 Firestore,但它保存為 0。我能夠從文本文件中成功保存名稱和地址,但不能保存經度和緯度。 我是新開發人員,但在傳遞數據時遇到問題。
這是我初始化地址、經度和緯度的 class。
class VendorLocation: NSObject, Codable {
var address: String
var latitude: Double
var longitude: Double
init(address: String, latitude: Double, longitude: Double){
self.address = address
self.latitude = latitude
self.longitude = longitude
}
convenience override init() {
self.init(address: "", latitude: 0.0, longitude: 0.0)
}
}
這是為每個選定位置設置表視圖的 ViewController。 一旦點擊具有所選位置的單元格,它將轉到下一個視圖 controller。
class LocationListViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var editBarButton: UIToolbar!
@IBOutlet weak var addBarButton: UIBarButtonItem!
var vendorLocations: [VendorLocation] = []
var selectedLocationIndex = 0
var vendorLocation: VendorLocation!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowDetail"{
let destination = segue.destination as! SpotDetailViewController
let selectedIndexPath = tableView.indexPathForSelectedRow!
destination.vendorLocation = vendorLocations[selectedIndexPath.row]
selectedLocationIndex = tableView.indexPathForSelectedRow!.row
// vendorLocations[tableView.indexPathForSelectedRow!.row]
}
}
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
// Display the autocomplete view controller.
present(autocompleteController, animated: true, completion: nil)
}
@IBAction func editBarButtonPressed(_ sender: UIBarButtonItem) {
if tableView.isEditing{
tableView.setEditing(false, animated: true)
sender.title = "Edit"
addBarButton.isEnabled = true
} else {
tableView.setEditing(true, animated: true)
sender.title = "Done"
addBarButton.isEnabled = true
}
}
}
extension LocationListViewController: UITableViewDataSource, UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return vendorLocations.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = vendorLocations[indexPath.row].address
cell.detailTextLabel?.text = "Lat: \(vendorLocations[indexPath.row].latitude), Long::\(vendorLocations[indexPath.row].longitude)"
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath){
if editingStyle == .delete {
vendorLocations.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath){
let itemToMove = vendorLocations[sourceIndexPath.row]
vendorLocations.remove(at: sourceIndexPath.row)
vendorLocations.insert(itemToMove, at: destinationIndexPath.row)
}
}
extension LocationListViewController: GMSAutocompleteViewControllerDelegate {
// Handle the user's selection.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
print("Place name: \(place.name)")
print("Place ID: \(place.placeID)")
print("Place attributions: \(place.attributions)")
let newLocation = VendorLocation(address: place.formattedAddress ?? "unknown place", latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
vendorLocations.append(newLocation)
tableView.reloadData()
dismiss(animated: true, completion: nil)
}
func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
// TODO: handle the error.
print("Error: ", error.localizedDescription)
}
// User canceled the operation.
func wasCancelled(_ viewController: GMSAutocompleteViewController) {
dismiss(animated: true, completion: nil)
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}
```swift
This is the ViewController from where the name and address are saved to Firestore successfully but longitude and latitude are saved as zero.
```swift
class SpotDetailViewController: UIViewController {
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var addressTextField: UITextField!
let db = Firestore.firestore()
let auth = Auth.auth()
var vendorLocation: VendorLocation!
var vendorLocations: [VendorLocation] = []
var vendorAddress: String!
//retrurn zero
var lat = VendorLocation().latitude.self
var long = VendorLocation().longitude.self
override func viewDidLoad() {
super.viewDidLoad()
if vendorLocation == nil{
vendorLocation = VendorLocation()
}
updateUserInterface()
}
func updateUserInterface () {
addressTextField.text = vendorLocation.address
}
func updateFomInterface() {
vendorLocation.address = addressTextField.text ?? "No address"
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination as! LocationListViewController
destination.vendorLocations = vendorLocations
}
@IBAction func unwindFromLocationListViewController(segue: UIStoryboardSegue) {
let source = segue.source as! LocationListViewController
vendorLocations = source.vendorLocations
vendorLocation = vendorLocations[source.selectedLocationIndex]
updateUserInterface()
}
@IBAction func saveLocationButtonPressed(_ sender: UIButton) {
updateFomInterface()
if let currentuser = (auth.currentUser?.uid), let vendorName = nameTextField.text, let vendorAddress = addressTextField.text {
db.collection("Vendor").document(currentuser).collection("Locations").addDocument(data: ["name": vendorName, "address": vendorAddress, "latiude": lat, "longtitude": long]) { (error) in
if let e = error {
print("For some reason the data can not be saved to the cloud! \(e)")
} else {
print ("The data was saved successfully!")
}
}
}
}
}
這段代碼沒有意義:
//retrurn zero
var lat = VendorLocation().latitude.self
var long = VendorLocation().longitude.self
構造VendorLocation()
調用VendorLocation
的初始化程序來創建一個新的 VendorLocation,它沒有有效的地址、緯度或經度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.