我需要一些帮助来填充谷歌地图标记,方法是使用我的Mongodb上的NodeJS数据。
这是我的Model Schema(models / listing.js):
var restful = require('node-restful');
var mongoose = restful.mongoose;
// Schema
var listingSchema = new mongoose.Schema({
category: String,
title: String,
location: String,
latitude: Number,
longitude: Number,
url: String,
type: String,
type_icon: String
},
{ collection: 'listing' }
);
// Return Model
module.exports = restful.model('Listing', listingSchema);
当我使用postman进行GET / api / listing时,这就是我所拥有的
[{
"_id": "57092ca64f43442f0bcd6a95",
"category": "services",
"title": "Musa 24 hours Printing",
"location": "16 Bali Lane, Singapore 189852",
"latitude": 1.3007598,
"longitude": 103.8588499,
"url": "http://www.musa-group.com/24hrsinternet/printing.html",
"type": "Printing",
"type_icon": "assets/icons/media/text.png",
"gallery": [
"http://i.imgur.com/HwiyMCK.png"
]},
{
"_id": "57092ca64f43442f0bcd6a96",
"category": "services",
"title": "Rocket Printers SG",
"location": "146 Jalan Bukit Merah, Singapore 160146",
"latitude": 1.2778769,
"longitude": 103.8308443,
"url": "http://www.rocketprinters-sg.com/",
"type": "Printing",
"type_icon": "assets/icons/media/text.png",
"gallery": [
"http://i.imgur.com/XPYgZ7a.jpg"
]
}]
在我的index.ejs文件中,标记当前是从items.json.txt文件中提取的
<script>
var _latitude = 1.36080344;
var _longitude = 103.81565094;
var jsonPath = 'assets/json/items.json.txt';
// Load JSON data and create Google Maps
$.getJSON(jsonPath)
.done(function(json) {
createHomepageGoogleMap(_latitude,_longitude,json);
})
.fail(function( jqxhr, textStatus, error ) {
console.log(error);
});
// Set if language is RTL and load Owl Carousel
$(window).load(function(){
var rtl = false; // Use RTL
initializeOwl(rtl);
});
autoComplete();
</script>
如何更改来自&#39; items.json.txt&#39;的来源?到我的上市&#39;数据库集合?非常感谢任何帮助!
答案 0 :(得分:0)
假设您的JSON文件与/ api / listing返回的JSON具有相同的结构,您可以简单地用 yourserver.com:XX/api/listing 替换JSON文件的URL,假设服务器 yourserver.com 正在端口 XX 上运行您的API。
我怀疑jQuery.getJson方法只是jQuery.get的一个包装器,它为请求添加了参数,例如相应的Content-Type
标头。