【问题标题】:Populating Google Map Markers from Node Mongodb model从 Node Mongodb 模型填充谷歌地图标记
【发布时间】:2016-07-30 22:46:30
【问题描述】:

我需要一些帮助来使用我的 Mongodb 上的数据和 NodeJS 来填充谷歌地图标记。

这是我的模型架构 (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);

当我使用邮递员获取 /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>

如何将源从“items.json.txt”更改为“列表”数据库集合?非常感谢任何帮助!

【问题讨论】:

    标签: json node.js mongodb google-maps-markers


    【解决方案1】:

    假设您的 JSON 文件与 /api/listing 返回的 JSON 具有相同的结构,您可以简单地将 JSON 文件的 URL 替换为 yourserver.com:XX/api/listing,假设服务器 yourserver.com 正在端口 XX 上运行您的 API。

    我怀疑jQuery.getJson 方法只是jQuery.get 的包装器,它将参数添加到请求中,例如适当的Content-Type 标头。

    【讨论】:

      猜你喜欢
      • 2014-03-23
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多