简体   繁体   English

使用流星框架处理地理定位

[英]Handling geolocation with meteor framework

How can I handle geolocation success and failure events within the meteor framework? 如何在流星框架内处理地理位置成功和失败事件? More specifically, the call "navigator.geolocation.getCurrentPosition(showPosition);" 更具体地说,调用“ navigator.geolocation.getCurrentPosition(showPosition);” says to call showPosition if there was success in getting the location. 说如果成功获取位置,请致电showPosition。 Whenever I define a generic javascript function (function showPosition(position) {}) I get a web socket error. 每当我定义通用javascript函数(函数showPosition(position){})时,都会收到Web套接字错误。 So how do I call these functions within the meteor framework? 那么如何在流星框架内调用这些函数呢?

Check the module mdg:geolocation 检查模块mdg:geolocation

meteor add mdg:geolocation

After use it in your code: 在您的代码中使用它之后:

Geolocation.currentLocation()

This isn't a meteor feature specifically. 这不是专门的流星功能。 It works with geolocation compatible browsers. 它可与地理位置兼容的浏览器一起使用。 Once you get the location you can send it or save it via Meteor. 获取位置后,您可以通过Meteor发送或保存它。

Client side js: (make sure the collection exists on the server) 客户端js :(确保集合在服务器上存在)

if (navigator.geolocation)
{
    navigator.geolocation.getCurrentPosition(showPosition);
}
   else{
       console.log("Geolocation is not supported by this browser.";
   }
}

function showPosition(position)
{
    Locations.insert({latitude: position.coords.latitude, 
                      longitude: position.coords.longitude});
}

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

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