【问题标题】:Saving Google Maps marker into Firebase db to be deleted later将 Google 地图标记保存到 Firebase 数据库中,以便稍后删除
【发布时间】:2021-02-14 17:51:54
【问题描述】:

我正在开发一个使用 Google Maps Api 的网络应用程序。这是我的问题 我试图将一些 Google 标记保存到 Firebase 数据库中,以便在我的网络中的不同位置检索它们 应用程序,但是当我尝试保存它们时,Gooogle Firebase 会引发错误。

var main_pts                            = {}; 
var location1                           = {}; 
location1.latitude                      = 'Random lat'; 
location1.longitude                     = 'Random long'; 

var location2                           = {}; 
location2.latitude                      =  'Random lat'; 
location2.longitude                     = 'Random long'; 

main_pts.location1                      = location1; 
main_pts.location2                      = location2; 

function create_main_vendor_pts(map){
    var main_Google_Markers = []; 
    var marker1             = new google.maps.Marker({
        position: { lat: main_pts.location1.latitude, 
            lng: main_pts.location1.longitude }, 
        icon: {
            labelOrigin: new google.maps.Point(16, 64),
            url: "https://drive.google.com/uc?id=0B3RD6FDNxXbdVXRhZHFnV2xaS1E"
        },
        label: {
            text        : 'Random', 
            color       : "yellow",
            fontWeight  : "bold",
            fontSize    : "20px"
        }
    }); 
    var marker2             = new google.maps.Marker({
        position: { lat: main_pts.location2.latitude, 
            lng: main_pts.location2.longitude }, 
        icon: {
            labelOrigin: new google.maps.Point(16, 64),
            url: "https://drive.google.com/uc?id=0B3RD6FDNxXbdVXRhZHFnV2xaS1E"
        },
        label: {
            text        : 'Random2', 
            color       : "yellow",
            fontWeight  : "bold",
            fontSize    : "20px"
        }
    });
    main_Google_Markers.push(marker1);
    main_Google_Markers.push(marker2);   
    add_Google_Markers_dB(main_Google_Markers);

}

function add_Google_Markers_dB(markers){
    var db_object                       = {};
    for(var i = 0; i < markers.length; i ++){
        db_object[`marker_${i}`] =  markers[i];
    }
    //console.log(db_object); 
    var db              = firebase.firestore();
    var db_reference    = db.collection('vendor_points_scheduled').doc('Main_Vendor_pts'); 
    db_reference.set(db_object).then(()=>{
        console.log("Data is in... "); 
    })
}

请注意,我想将 Google 标记保存到 Firebase 以供以后检索,而不是它们的位置.. 等等。我能够通过保存获得我想要的行为 main_Google_Markers 作为全局变量。但是我想要谷歌标记,因为这段代码被多次调用,使得全局变量被初始化

main_Google_Markers = []

【问题讨论】:

  • 请不要张贴错误信息的图片。将它们作为文本发布在您的问题中(将它们包含在标题中也很有用)。

标签: javascript firebase google-maps google-cloud-firestore


【解决方案1】:

Firestore 只能存储其文档data types 中描述的数据类型。您正在尝试存储 google.maps.Marker 对象,显然包含无法映射到这些类型的数据。

您应该存储创建此类对象所需的信息,而不是尝试存储google.maps.Marker 对象本身。然后,您可以在从 Firestore 加载数据时重新创建标记。

请注意,这个问题之前有人问过,所以我建议您阅读searching for the error message时得到的一些结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    • 2014-06-03
    相关资源
    最近更新 更多