2013-07-19 83 views
0

我有一个位置字段的用户提交表单。我想调用Google地理编码API并获取位置的纬度和经度以确保位置存在,然后将所有三个数据发送到数据库。如何在Meteor应用程序表单提交中实现地理编码?

geocoder = new google.maps.Geocoder(); 
geocoder.geocode({ 'address': location}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
    alert(results[0].geometry.location); 
    } else { 
    alert('Geocode was not successful for the following reason: ' + status); 
    } 
}); 

我可以找得到这个代码,以提醒正确的坐标的唯一地方是在我的收藏posts.js文件,但它也导致了服务器错误:

Exception while invoking method 'post' ReferenceError: google is not defined 
+0

您是否在您的' ..'部分引用了google apis? – Akshat

+0

是的,坐标是进来的。我只是不知道我应该把地理编码放在我的文件中,以及如何防止服务器错误。 –

+0

哦,我明白了,你正在服务器上运行它?您需要使用某种服务器端插件,例如(npm)-https://npmjs.org/package/geocoder,因为Google地址解析器API仅适用于客户端 – Akshat

回答

0

地理编码应可以在添加帖子页面管理器(仅限客户端)中完成。由于地理编码调用是异步的,因此在发布到数据库之前我没有收到响应。移动地理编码功能的成功分支内的数据库文章修复了它。

geocoder = new google.maps.Geocoder(); 
geocoder.geocode({ 'address': pin.location}, function(results, status) { 
if (status == google.maps.GeocoderStatus.OK) { 
     pin.lat = results[0].geometry.location.lat(); 
     pin.lng = results[0].geometry.location.lng(); 

     Meteor.call('post', pin, function(error, id){ 
      if (error) 
       return alert(error.reason); 

      Meteor.Router.to('pinPage', pin); 
     }); 
    } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
    } 
}); 
相关问题