- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在使用地理位置 navigator.geolocation.getCurrentPosition
但发现不如 navigator.geolocation.watchPosition
准确。所以我的想法是让运行navigator.geolocation.watchPosition
直到准确度达到 <=100
然后显示位置,如果15秒内失败则报错。
这是我得到的:
function getLocation() {
if (navigator.geolocation) {
var geo_options = {
enableHighAccuracy: true,
timeout: 15000,
maximumAge: 0
};
var watchID = navigator.geolocation.watchPosition(
showPosition,
showError,
geo_options
);
} else {
// Error here. Geolocation disabled.
}
}
function showPosition(position) {
if (position.coords.accuracy > 100) {
// Keep trying
} else {
// Fire up the map, we got a position!
// Clear the watchID.
navigator.geolocation.clearWatch(watchID);
}
}
我的问题是,由于某种原因,我无法在成功时清除 watchID,因为它说它是未定义的。我猜那是因为该函数位于外部。
有没有一种简单的方法可以做到这一点,以便 showPosition
仅当准确度低于 100 时才触发?现在showPosition
随时会被触发,因为它位于 watchPosition
内功能。
最佳答案
那是因为 watchID
是 未定义
。您已将其定义在 getLocation
范围内,该范围不与 showPosition
共享其范围。尝试在它们之外声明它。
var watchID;
function getLocation() {
...
watchID = navigator.geolocation.watchPosition(showPosition, showError, geo_options);
}
function showPosition(position) {
if (position.coords.accuracy <= 100) {
navigator.geolocation.clearWatch(watchID);
}
}
专业提示:如果您的代码在 strict mode 中运行它会提醒您此错误。
'use strict';
function declareX() {
var x = 1;
}
function useX() {
console.log(x);
}
declareX();
useX();
关于javascript - 如何运行 Geolocation watchPosition 直到获得准确的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39987253/
我正在编写需要获取设备位置的 Windows Phone 8 应用(不跟踪更改,只获取位置)。我在起始页的 OnNavigatedTo() 方法中添加了下一个代码,但在启动应用程序后,进度指示器即使在
工作灯 6.2.0.00-20120814-0824 位置代码在浏览器模拟器中运行良好,但在 Android 模拟器中失败,成功或失败回调均未触发。 Logcat 显示: 10-06 16:00:40
我向 google geolocation api 发送了两个请求:https://www.googleapis.com/geolocation/v1/geolocate?key=[mykey] 第一
Type 'GeolocationOriginal' is not assignable to type 'Provider'. Type 'GeolocationOriginal' is missi
我正在开发一个带有谷歌地图的网络应用程序,并使用 getCurrentPosition() 获取用户位置。这很好用,但我需要随着时间的推移跟踪用户位置。 为此,我打算使用 watchPosition(
我正在尝试搜索一个地址,然后尝试通过 Geolocator 获取该位置的位置,然后尝试将相机位置设置到该位置,但它不起作用。 我发现了 Geolocator 的两种方法的一些异常(exception)
我在 Windows 商店单元测试项目中进行了此单元测试,使用 this overload of GetGeopositionAsync地理定位器: [TestMethod] public async
这是我的情况;我构建了一个非常简单的网络应用程序,用于查找用户位置并将其绘制在 Google map 上。这是我的代码:http://pastebin.com/d3a185efd 当我测试它时,我的位
我有一个表,其中包含位置列数据作为经纬度信息,如何获取多边形内位置的记录 多边形点 [ [42.811521745097906, -105.60058593749999], [33.9
获取网络请求来源位置(国家级别)的最佳方式是什么? 最佳答案 一般来说有3种方式: 使用 GeoIP 或将 IP 映射到物理位置的一些类似数据库或服务 使用 W3C 地理定位 API(仅受较新的浏览器
我们一直需要为我们的一些产品实现一些地理定位服务集成。有很多 3rd 方公司提供免费和付费的地理位置服务和数据库,这些服务和数据库有时会不断更新,有时会每月更新一次。 这些服务从哪里获得他们分发的数据
如何使用经度和纬度找到最近的机场? 任何特定的网络服务和任何数据库来实现? 最佳答案 我发现的一个 WebService 是 airport.pidgets.com 这是一个例子: XML 格式 ht
在许多网站上,我看到打印出我当前所在的城市(例如“Hello to Berlin.”)。他们是怎么做到的?为此需要什么? 我想主要部分是这里的 javascript,但是在我自己的应用程序中实现这样的
大家好, 我正在创建一个使用 Geolocation API 来定位最终用户的 Web 应用程序。它在我能想到的几乎所有平台上都运行良好,除了 Internet Explorer 9。不过事情变得有点
我目前正在使用“区域”示例代码: https://developer.apple.com/library/ios/#samplecode/Regions/Introduction/Intro.h tm
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 3年前关闭。 Improve thi
我在筛选 twitter API 时遇到了一些麻烦。我正在尝试搜索某个位置附近的推文,然后查看它们的精确(或近似)地理坐标。我知道 geo 字段已被弃用,现在我们应该使用“place”字段。不幸的是,
我需要能够评估一个位置的地理坐标有多远。我根据几个关键指标对远程度进行评分,到目前为止,我只能计算所有必需指标的一个子集: 给定坐标处的蜂窝接收。更具体地说,坐标周围的蜂窝塔密度。这可以使用 open
我正在开发GeoTargeting应用程序。我很好奇地球上某个点的经度和纬度是否可以改变? 如果您知道自由女神像的确切位置,那么如何确定经度和纬度将保持不变。 它是根据季节,一年中的时间还是随着时间的
由于Cassandra基于Dynamo纸(分布式,自平衡哈希表)+ BigTable,因此存在一些空间索引可以很好地适合该范式(quadkey或geohash)。是否有未实现地理空间支持的原因? 您可
我是一名优秀的程序员,十分优秀!