简体   繁体   English

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

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

I am trying to load the Google Maps javascript API on the server side of my meteor app. 我正在尝试在流星应用程序的服务器端加载Google Maps javascript API。 I added two methods to Meteor.methods that use the google.maps but no matter how I try to load the api, I receive the error Exception while invoking method 'getCoords' ReferenceError: google is not defined . 我向使用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 I understand that I cannot use the <src> tag to load the api, as I was using when I was prototyping with the API on the client side. 我了解我无法使用<src>标记来加载api,就像我在客户端使用API​​进行原型制作时所使用的那样。 I also tried using Iron Router with the waitOn library like so: 我也尝试将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')]
  }
 });
});

The relevant methods I have created are: 我创建的相关方法是:

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);
        }
    });

If you have any insights, I would greatly appreciate them! 如果您有任何见解,我将不胜感激! Thank you! 谢谢!

you have to remember that meteor is a framework based on node.js. 您必须记住,流星是基于node.js的框架。 The server side is node.js. 服务器端是node.js。 So in order to load a library, your best bet is to look at how to load a library in node.js. 因此,为了加载库,最好的选择是查看如何在node.js中加载库。

require does not allow you to load a remote file, but there seems to be ways around that. require不允许您加载远程文件,但是似乎有解决方法。 Have a look at this answer how to require from URL in Node.js 看看这个答案如何从Node.js中的URL要求

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

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