响应式html5地理定位
Meteor.js reactive html5 geolocation position.coords
我是一个meteor.js爱好者,但我确实花了相当多的时间来弄清楚这个。
我试图根据传入getCurrentPosition的HTML5地理位置API回调来主动更新我的UI。但是,UI没有更新我当前的代码。
你能提供建议和/或解决方案吗?以下是详细信息:
基础:meteor server正在运行,通过collection成功地向mongo提供其他数据
我有一个主页(main.html):
<head>
<title>Geolocation</title>
</head>
<body>
<div class="container">
<header class="navbar">
<div class="navbar-inner">
<a class="brand" href="/">Geolocation</a>
</div>
</header>
<div id="locationDemo">
{{> location}}
</div>
</div>
</body>
引用模板(location.js)的:
<template name="location">
<div>
Lat: {{lat}}
</div>
<div>
Lon: {{lon}}
</div>
</template>
有这个关联的帮助器(location.js):
_lat = {
current: 0,
dep: new Deps.Dependency,
get: function(){
this.dep.depend();
if(this.current === 0){
getLocation();
}
return this.current;
},
set: function(value){
this.current = value;
this.dep.changed();
Deps.flush();
return this.current;
}
};
_lon = {
current: 0,
dep: new Deps.Dependency,
get: function(){
this.dep.depend();
if(this.current === 0){
getLocation();
}
return this.current;
},
set: function(value){
this.current = value;
this.dep.changed();
Deps.flush();
return this.current;
}
};
function getLocation(){
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition, showError);
}
else{
console.log("Geolocation is not supported by this browser.");
}
}
function showPosition(position)
{
_lat.set(position.coords.latitude);
_lon.set(position.coords.longitude);
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
console.log("User denied the request for Geolocation.");
break;
case error.POSITION_UNAVAILABLE:
console.log("Location information is unavailable.");
break;
case error.TIMEOUT:
console.log("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
console.log("An unknown error occurred.");
break;
}
}
Template.location.helpers({
lat: _lat.get(),
lon: _lon.get()
});
据我所知,navigator.geolocation
不是一个响应式数据源。因此,如果没有一些显式轮询,这将无法工作。另一个你犯的错误是你的helper不是函数,所以它们不能被反复调用。
这可能会工作(未测试):
Meteor.setInterval(function() {
navigator.geolocation.getCurrentPosition(function(position) {
Session.set('lat', position.coords.latitude);
Session.set('lon', position.coords.longitude);
});
}, 5000);
Template.location.helpers({
lat: function() { return Session.get('lat'); },
lon: function() { return Session.get('lon'); }
});
要使地理定位在编译为原生iOS应用的Meteor应用中工作,我需要添加可从https://atmospherejs.com/mdg/geolocation
相关文章:
- 如何用HTML5地理定位API实现承诺
- 如何强制HTML5地理定位失败
- 当使用HTML5地理定位(使用Meteor js)时,谷歌地图不会显示在Firefox上
- HTML5:矩形定位不准确
- 在我的网页上定位 Canvas HTML5 游戏时遇到问题
- Qt WebKit和HTML5地理定位
- 移动Android设备上的HTML5地理定位行为
- 点击html5地理定位当前位置,然后减去两点
- 谷歌地图Api HTML5地理定位与安卓定位Api
- 手机在安卓系统上唤醒后,带有GPS的HTML5地理定位失败
- 猜测HTML5地理定位何时能正常工作
- HTML5画布:在画布外生成随机定位的对象
- 通过HTML5地理定位API以外的方式识别用户的地理位置
- Html5地理定位在谷歌地图api
- Html5地理定位不能在safari浏览器上工作
- HTML5获取地理定位功能
- 高精度地理定位Html5
- 地理定位HTML5 API enableHighAccuracy:True不强制GPS在Android设备上打开
- 如何大小和定位html5画布在javascript
- 当改变文本框中的X和Y时,重新定位HTML5 Canvas上的元素