如何在我的数据库中保存纬度、经度.使用传单地图



我正在构建一个使用传单地图和mongoDB作为数据库的应用程序。我希望用户能够点击地图上的一个地方并编辑标记上的详细信息,然后我想将这些地方保存在我的数据库中。我该怎么做?我是一个相对较新的人,我的文凭论文必须这样做,我以前没有数据库和javascript的经验。我已经用猫鼬设置了数据库。

我在stackoverflow上搜索了一个类似的问题,但我找不到任何新的问题,如果我只是错过了,请重定向到那里。

谢谢!

将数据保存到数据库中。

// 1. make a mongoose model
const schema = new mongoose.Schema({
name: {
type: String,
default: "Placeholder Location Name"
},
coordinates: {
type: [Number],
default: [0, 0]
}
});
const Location = mongoose.model('Location', schema);
// 2. example of making a Location
const exampleLocation = new Location({
name: "My First Location",
coordinates: [41.40338, 2.17403] // This is where your example coordinates go.
})
exampleLocation.save((err) => {
if (err) console.log("An error occured while trying to save: " + err)
else console.log("Success") // the object is saved
})

捕捉点击地图

let map = document.getElementById('your-leaflet-map-id')
map.addEventListener('click', (event) => {
console.log('The clicked coordinates were: ' + event.latlng.lat + ',' + event.latlng.lng)
// feel free to use these coordinates as you wish
yourMethodToSaveLocation(event.latlng.lat, event.latlng.lng)
})

最新更新