【问题标题】:How to load Google Maps API in MeteorJS Server?如何在 MeteorJS 服务器中加载 Google Maps API?
【发布时间】:2015-11-04 10:46:50
【问题描述】:

我正在尝试在我的流星应用程序的服务器端加载 Google Maps javascript API。我向Meteor.methods 添加了两种使用google.maps 的方法,但无论我如何尝试加载api,我都会收到错误Exception while invoking method 'getCoords' ReferenceError: google is not defined。我知道我不能使用 <src> 标签来加载 api,就像我在客户端使用 API 进行原型设计时使用的那样。我还尝试使用 Iron Router 和 waitOn 库,如下所示:

Router.map( function () {
this.route('codeEditor',{
  waitOn: function(){
      return [IRLibLoader.load('https://maps.googleapis.com/maps/api/js?libraries=places,geometry?key=MyKey')]
  }
 });
});

我创建的相关方法有:

Meteor.startup(function() {
    Meteor.methods({
    getCoords: function(startLocation,endLocation,priceFilter) {
        var directionsService = new google.maps.DirectionsService();
        var path = []
        var request = {
          origin:startLocation,
          destination:endLocation,
          travelMode: google.maps.TravelMode.DRIVING
        };
        directionsService.route(request, function(result, status) {
          if (status == google.maps.DirectionsStatus.OK) {
          path = result.routes[0].overview_path;
          Meteor.call('samplePath',path);
        }
    });

如果您有任何见解,我将不胜感激!谢谢!

【问题讨论】:

  • 什么是IRLibLoader
  • 另外,您正在客户端而不是服务器上执行load,因为您定义的路由是客户端路由,而不是服务器路由。
  • @ChristianFritz IRLibLoader 据说是一种加载外部 JS 脚本的方法。它包含在包manuelschoebel:wait-on-lib 中。我尝试在路由中添加{where: 'server'} 选项,但这似乎并没有解决问题。看来我有很多关于铁路由器的知识,但是由于这应该是一个单页应用程序,如果我能找到一种不带包加载这个JS的方法,我会更喜欢它。
  • @DrGorb 您的链接看起来很有希望,但在支持的 api 页面 (developers.google.com/apis-explorer/#p) 中显示仅支持坐标 api 和地图引擎 api,这让我相信我不能拥有我想要的全部功能。

标签: javascript google-maps google-maps-api-3 meteor


【解决方案1】:

你必须记住,meteor 是一个基于 node.js 的框架。服务器端是 node.js。因此,为了加载库,最好的办法是查看如何在 node.js 中加载库。

require 不允许您加载远程文件,但似乎有办法解决这个问题。看看这个答案how to require from URL in Node.js

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2011-07-14
    • 2011-03-15
    • 2011-08-05
    相关资源
    最近更新 更多