【问题标题】:Geolocation.currentLocation() not working, giving null value using meteor(1.5.1) & mdg:geolocation(1.3.0)Geolocation.currentLocation()不起作用,使用meteor(1.5.1)和mdg:geolocation(1.3.0)给出空值
【发布时间】:2017-08-02 09:50:03
【问题描述】:
我在一个流星项目 (1.5.1) 中工作并使用 mdg:geolocation@1.3.0,我正在尝试获取 Geolocation.currentLocation() 的值,但它给了我空值并且行为是始终不一样,有时它给我空值,有时给我适当的值。
我研究了很久,但还没有得到解决方案,如果可能请提供meteor@1.5.1的解决方案,并使用mdg:geolocation包。
提前谢谢...
【问题讨论】:
标签:
google-maps
meteor
geolocation
currentlocation
【解决方案1】:
MDG 已实现mdg:geolocation 用于连续地理定位,并且此 Meteor 包中的函数返回一个反应变量。您可以通过这种方式使用包(例如):
// Continous geolocation with mdg:geolocation
// This code will run every time user location changes.
Tracker.autorun(() => {
const position = Geolocation.currentLocation();
Tracker.nonreactive(() => {
if (position) {
Meteor.call('user.update.position', position);
}
});
});
如果你不需要连续的地理定位,你可以使用javascript方法代替meteor包。有关windows.navigator.geolocation 的其他选项,请参阅this answer。在这种情况下,您可能需要添加 cordova-plugin-geolocation 以防您正在构建 Android 或 iOS 应用程序
// Navigation geolocation to get geolocation only once
let errorCallback;
let successCallback;
successCallback = position => Meteor.call('user.update.position', position);
errorCallback = err => console.log(err);
navigator.geolocation.getCurrentPosition(successCallback, errorCallback, {
maximumAge: 60000,
timeout: 20000
});