【问题标题】:How to add GeoPoint to firebase collection in asp.net core如何将 GeoPoint 添加到 asp.net core 中的 firebase 集合
【发布时间】:2022-11-30 05:20:51
【问题描述】:

我可以将字符串添加到 Firebase 集合,但我需要将经度和纬度添加到具有地理点数据类型的字段。

我如何通过 long 和 lat12.146644,-68.277466保存在集合中

 public async Task<string> CreateData(Data data)
        {
            try
            {
               
                CollectionReference collectionReference = _firestoreDb.Collection("Data");
                var response = await collectionReference.AddAsync(data);
                return response.ToString();
            }
            catch (Exception ex )
            {
                return ex.Message;
            }
          
        }    

POCO

[FirestoreData]
    public class Data
    {
    
        [FirestoreProperty]
        public string username{ get; set; } 
        [FirestoreProperty]
        public string name { get; set; } 
        [FirestoreProperty]
        public string loglat{ get; set; }**//this represents the concatenated value of long and lat** 
        [FirestoreProperty]
        public string street { get; set; }
       
    }

【问题讨论】:

    标签: c# firebase asp.net-core google-cloud-firestore


    【解决方案1】:

    所以我将属性 loglat 设为一种 GeoPoint

      [FirestoreData]
        public class Data
        {
        
            [FirestoreProperty]
            public string username{ get; set; } 
            [FirestoreProperty]
            public string name { get; set; } 
            [FirestoreProperty]
            public GeoPoint loglat{ get; set; }**//this represents the concatenated value of long and lat** 
            [FirestoreProperty]
            public string street { get; set; }
           
        }
    
    

    所以

     public async Task<string> CreateData(DataDTO data2)
            {
                try
                {
                    var lat = double.Parse(data2.latitude);
                    var lon = double.Parse(data2.longtitude);
    
                    GeoPoint geoPoint = new GeoPoint(lat, lon);
    
                    Data data = new Data
                    {
                        username = data2.username,
                        name = data2.name,
                        loglat = geoPoint,
                        street = data2.street,
    
                    };
    
                    CollectionReference collectionReference = _firestoreDb.Collection("Data");
                    var response = await collectionReference.AddAsync(data);
                    return response.ToString();
                }
                catch (Exception ex )
                {
                    return ex.Message;
                }
              
            }  
    
    

    【讨论】:

      猜你喜欢
      • 2019-06-16
      • 1970-01-01
      • 2019-03-01
      • 2018-05-10
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      相关资源
      最近更新 更多