如何在我的数据库中保存纬度和经度使用单张Map

2vuwiymt  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(167)

我正在构建一个应用程序,使用传单Map和mongodb作为我的数据库。我希望用户能够单击Map上的某个位置并编辑标记上的详细信息,然后我希望将这些位置保存到我的数据库中。我该怎么做?我是一个比较新的人,我必须为我的毕业论文做这件事,我以前没有数据库和javascript方面的经验。我已经使用mongoose设置了我的数据库。
我在stackorverflow上搜索了一个类似的问题,但我找不到任何新的问题,如果我错过了,请将我重定向到那里。
谢谢

wswtfjt7

wswtfjt71#

将数据保存到数据库中。

// 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
})

捕捉Map上的点击

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)
})

相关问题