- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经浏览了几个小时来寻找解决方案。我对 JavaScript 的了解很少,我实际上正在学习它。
我在获取用户的 GPS 位置时遇到问题。我想 :- 获取用户的位置(jQuery)。- 通过Ajax > PHP传输到服务器端。
这是我的 JS 代码:
$(document).ready(function () {
initCoords();
});
function initCoords() {
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(updateLocation, errorHandler, {enableHighAccuracy: false, maximumAge: 60000, timeout: 27000});
} else
{
alert('Wrong browser.');
}
}
function updateLocation(position)
{
var longitude=position.coords.longitude;
var latitude=position.coords.latitude;
console.log(longitude);
$.ajax({
url: "../../ajax/account/handlegeolocation",
type: "post",
dataType: "json",
data: {"longitude": longitude, "latitude": latitude},
success: function (response) {
console.log(response.message);
},
error: function (xmlhttprequest, textstatus, message) {
console.log(message);
}
}).then(function () {
setTimeout(updateLocation, 30);
});
}
function errorHandler(error)
{
console.log('Geolocation error : code ' + error.code + ' - ' + error.message);
}
令我惊讶的是加载页面时看到的内容。返回了位置,但是JS说返回位置的对象未定义。
感谢您的帮助!
最佳答案
这是因为您没有从
传递位置对象setTimeout(updateLocation, 30);
超时调用函数initCoords
将解决您的问题。
或者这是您的代码,经过一些编辑并且没有任何错误。
$(document).ready(function () {
initCoords();
});
function initCoords() {
if (navigator.geolocation) {
getGeoLocation();
} else {
alert('Wrong browser.');
}
}
function getGeoLocation() {
navigator.geolocation.getCurrentPosition(updateLocation, errorHandler, { enableHighAccuracy: false, maximumAge: 60000, timeout: 27000 });
}
function updateLocation(position) {
var longitude = position.coords.longitude;
var latitude = position.coords.latitude;
$.ajax({
url: "../../ajax/account/handlegeolocation",
type: "post",
dataType: "json",
data: { "longitude": longitude, "latitude": latitude },
success: function (response) {
console.log(response.message);
},
error: function (xmlhttprequest, textstatus, message) {
console.log(message);
}
}).then(function () {
setTimeout(getGeoLocation, 30);
});
}
function errorHandler(error) {
console.log('Geolocation error : code ' + error.code + ' - ' + error.message);
}
关于javascript - jQuery 地理定位 : Cannot read property 'coords' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46124994/
我正在尝试制作一个迷宫游戏。 我有“M”的怪物和“*”的宝藏。 如果怪物“M”吃掉了宝藏“*”,我希望游戏打印“GAME OVER”并退出。 如何检查 if(monster[x][y] == 宝藏[x
目标 知道一个坐标点是否在另外两个坐标之间。 背景 我正在制作一个谷歌地图应用程序,我需要知道某个点是否在两个 LatLng 点(起点和终点)之间。 我正在寻找如下函数: var currentCoo
我是js新手。 我需要根据 滚动页面我的大学项目中的 tag href 属性。 ScrollTop 功能不起作用,因为我的项目是单页应用程序。我可以使用 window.scrollTo(x-coor
我正在调用 openweathermap 的网络服务,它在 map 上显示预报天气。 我正在解析其他标签,如天气、温度、风等。但是当我解析标签图标时,我在这个坐标上得到了错误:[weatherItem
我正在尝试使用 Win32 控制台 API 在 Windows 中为控制台应用程序实现进度条。我的问题是编译器给出了错误,error C2059: syntax error : 'constant'。
我看到了一个与我想到的问题类似的问题,但该解决方案对我不起作用,所以想在这里发帖以寻求帮助。 我正在根据有关地理空间数据的 DataCamp 类(class)进行练习。下面是我作为开始执行的代码: l
我正在尝试使用 oozie 中的以下内容获取当前时间戳: date ${coord:formatTime(coord:dateOffset(coord:nominalTime(), -1,
我正在为我的 cs 课编写一个 Game of life 程序,而且我的一切都很好(虽然我希望我现在使用不同的结构,但哦,好吧)。我遇到的问题是我无法正确读取我们的输入文件。它是这样设置的:第一个数字
coords.accuracy 是如何测量的,单位是什么? 目标是在查找位置时使用它来确定误差半径。 最佳答案 通常以米为单位表示。 Geolocation API specified by W3C说
我想要一个形状,比如 map 中特定区域的圆形。借助 Html 中的 area 标记,我可以做到这一点。但是我如何为 coords 属性赋值,以便我需要那个特定区域的形状? 最佳答案 使用 GIMP
这个问题在这里已经有了答案: making the in a tag visible during development (5 个答案) How to put border on ? (1 个
我试图制作html图像
最近,在 Stack Overflow 的人们(我非常感谢他们)的帮助下,我用 java 完成了我自己的 Tron 游戏重制版。然而,发生了一些事情,现在游戏不会结束,因为我为测试玩家是否交叉路径而创
这个问题在这里已经有了答案: How do you add marker to map using leaflet map.on('click', function) event handler (
我正在使用带有地理定位功能的 Google Maps API。一切都按预期工作,但我的控制台中不断出现此错误: Uncaught TypeError: Cannot read property 'co
我正在尝试使用 jQuery offset() 函数定位绝对定位的 div。这个想法是将它定位在与 DOM 的另一个元素的固定偏移处。这发生在具有多个嵌套 div 的相当复杂的环境中。 奇怪的是调用它
我已经浏览了几个小时来寻找解决方案。我对 JavaScript 的了解很少,我实际上正在学习它。 我在获取用户的 GPS 位置时遇到问题。我想 :- 获取用户的位置(jQuery)。- 通过Ajax
这个问题在这里已经有了答案: Pointer to class data member "::*" (18 个回答) 1年前关闭。 所以我一直在看一些东西,发现这个线程Aliasing struct
我可以获取自己的经度和纬度并将它们保存到 vars 中: var myLat = position.coords.latitude; var myLong = position.coords.long
我正在制作游戏 使用 Slick2D。当玩家进入有敌人的 window 下时,他们可以射击,并获得分数。除了射击之外,我已经完成了所有机械师。这是我关于它如何运作的“计划”。 当玩家到达窗口下方(程序
我是一名优秀的程序员,十分优秀!