我正在建立一个应用程序,使用小叶地图和mongoDB作为我的数据库。我希望用户能够点击地图上的一个地方,并编辑标记上的详细信息,然后我想将这些地方保存在我的数据库中。我该怎么做?我是一个相对较新的人,我必须为我的毕业论文做这件事,而且我以前没有数据库和javascript的经验。我已经使用mongoose设置了我的数据库。
我在stackorverflow上搜索了类似的问题,但我找不到任何新的东西,如果我只是错过了它,请将我重新定向到那里。
谢谢!
发布于 2021-07-10 23:44:08
将数据保存到数据库中。
// 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)
})https://stackoverflow.com/questions/68329141
复制相似问题