【发布时间】: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