0

I am trying to load the Google Maps javascript API on the server side of my meteor app. 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. 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. I also tried using Iron Router with the waitOn library like so:

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!

Ethan
  • 3
  • 1
  • what is `IRLibLoader`? – Christian Fritz Aug 12 '15 at 02:17
  • also, you are executing the `load` on the client, not the server, because the route you are defining is a client-side route, not a server route. – Christian Fritz Aug 12 '15 at 02:19
  • have you tried https://www.npmjs.com/package/googleapis – Micha Roon Aug 12 '15 at 19:21
  • @ChristianFritz IRLibLoader is supposedly a way to load external JS scripts. It is included in the package `manuelschoebel:wait-on-lib`. I tried adding the `{where: 'server'}` option to the route, but that did not seem to solve the problem. It seems I have a lot to learn about iron router, but since this is supposed to be a single-page app, if I could find a way to load this JS without the package, I would prefer it. – Ethan Aug 13 '15 at 01:13
  • @DrGorb Your link looks promising, but it appears in the supported api's page (https://developers.google.com/apis-explorer/#p/) that only the coordinates api and the maps-engine api are supported, which leads me to believe that I cannot have the full functionality I want. – Ethan Aug 13 '15 at 01:15

1 Answers1

0

you have to remember that meteor is a framework based on node.js. The server side is node.js. So in order to load a library, your best bet is to look at how to load a library in node.js.

require does not allow you to load a remote file, but there seems to be ways around that. Have a look at this answer how to require from URL in Node.js

Community
  • 1
  • 1
Micha Roon
  • 3,957
  • 2
  • 30
  • 48