STORING CO-ORDINATES
GeoJSON
A STANDARD FORMAT FOR STORING GEOGRAPHIC POINTS.
HERE,DATA IS STORED IN THE FORM OF A POINT , IN WHICH LONGITUDES COMES FIRST.
THE FORMAT:
{
"type" : "Point",
"coordinates" : [
-122.5,
37.7
]
}IN OUR MODELS(LISTING.JS) WE UPDATE THE SCHEMA OF LISTINGS.
WE ADD A NEW FIELD GEOMETRY ,SIMILAR TO THAT OF THE LOCATION FIELD IN THE DOCUMENT.
const citySchema = new mongoose.Schema({
name: String,
location: {
type: {
type: String, // Don't do `{ location: { type: String } }`
enum: ['Point'], // 'location.type' must be 'Point'
required: true
},
coordinates: {
type: [Number],
required: true
}
}
}); geometry: {
type: {
type: String, // Don't do `{ location: { type: String } }`
enum: ["Point"], // 'location.type' must be 'Point'
required: true,
},
coordinates: {
type: [Number],
required: true,
},
},
});
IN LISTING.JS(CONTROLLER)
module.exports.postNewListing = async (req, res, next) => {
let response = await geocodingClient
.forwardGeocode({
query: req.body.listing.location,
limit: 2,
})
.send();
let url = req.file.path;
let filename = req.file.filename;
const newListing = new Listing(req.body.listing);
newListing.owner = req.user._id;
newListing.image = { url, filename };
newListing.geometry = response.body.features[0].geometry;
let savedListing = await newListing.save();
console.log(savedListing);

Comments
Post a Comment