关于javascript:如何在MeteorJS Server中加载Google Maps API?

How to load Google Maps API in MeteorJS Server?

我正在尝试在流星应用程序的服务器端加载Google Maps javascript API。我在Meteor.methods中添加了两个使用google.maps的方法,但是无论我如何尝试加载api,我都会收到错误Exception while invoking method 'getCoords' ReferenceError: google is not defined。我知道我无法使用<src>标记来加载api,就像我在客户端使用API?性椭谱魇彼褂玫哪茄N一钩⑹越獻ron Router与waitOn库一起使用,如下所示:

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

我创建的相关方法是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
        }
    });

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


您必须记住流星是基于node.js的框架。服务器端是node.js。因此,为了加载库,最好的选择是查看如何在node.js中加载库。

require不允许您加载远程文件,但是似乎有一些解决方法。看看这个答案如何从Node.js中的URL要求