2016-04-09 26 views
1

我需要一些幫助來填充谷歌地圖標記,方法是使用Mongodb上的數據和NodeJS。從節點Mongodb模型填充Google地圖標記

這是我的模型架構(型號/ 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); 

當我使用郵遞員GET/API /上市,這是我

[{ 
"_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" 
    ] 
    }] 

在我的指數.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> 

拉着我怎樣才能更改源從「items.json.txt」到我的'清單'數據庫集合?非常感謝任何幫助!

回答

0

假設你的JSON文件具有相同的結構JSON由/ API /上市歸來,你可以簡單地通過yourserver.com:XX/api/listing替換您的JSON文件的URL,假設服務器yourserver .com正在運行您的API端口XX

我懷疑jQuery.getJson方法只是一個圍繞jQuery.get的包裝,它爲請求添加了參數,例如合適的Content-Type標頭。

+0

感謝它的工作!你真棒。 –