繁体   English   中英

如何在MeteorJS Server中加载Google Maps API?

[英]How to load Google Maps API in MeteorJS Server?

我正在尝试在流星应用程序的服务器端加载Google Maps javascript API。 我向使用google.maps的Meteor.methods添加了两个方法,但是无论我如何尝试加载api, Exception while invoking method 'getCoords' ReferenceError: google is not defined收到错误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);
        }
    });

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM