简体   繁体   English

我的地理位置(javascript)无法正常运行,我也不知道为什么

[英]my geolocation(javascript) does not work and I don't know why

I am eventually trying reverse geolocation, however before doing that I want to make sure my geolocation works fine but no luck with my code. 我最终尝试进行反向地理位置定位,但是在此之前,我想确保地理位置定位正常,但我的代码没有运气。

what I am doing is that when user clicks button, my javascript shows latitude and longitude numbers if user agrees with it. 我正在做的是,当用户单击按钮时,如果用户同意,我的javascript将显示纬度和经度数字。

$(".button").click(function(){
  var elMap =document.getElementById("showWhere");
  var msg ='Sorry, we were unable to get your location';

    if(Modernizr.geolocation){
    navigator.geolocation.getCurrentLocation(success, fail);
    elMap.textContent = 'Checking location...';
  } else {
    elMap.textContent = msg;
  }

    function success(position){
    msg = '<h3>Lng: <br>';
    msg += position.coords.longtitude + '</h3>';
    msg +='<h3>lat: <br>';
    msg+=position.coords.latitude + '</h3>';
    elMap.innerHTML = msg;
  }

    function fail(msg){
    elMap.textContent = msg;
    console.log(msg.code);
  }
 });

I thought it is my click function problem and remove this part but it still does not work. 我以为这是我的点击功能问题,请删除此部分,但仍然无法正常工作。 What did I miss? 我错过了什么? Thank you for in advance :D 预先感谢您:D

1) No need to use Modernizr. 1)无需使用Modernizr。

if ('geolocation' in navigator)

2) It's getCurrentPosition 2)是getCurrentPosition

navigator.geolocation.getCurrentPosition(success, fail);

3) Make sure you spell longitude correctly. 3)确保正确拼写longitude

DEMO 演示

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

相关问题 我的for循环不起作用,我也不知道为什么 - my for loop doesn't work and I don't know why Ajax在这种情况下不起作用,我也不知道为什么 - Ajax does not work in this case and I don't know why 如果更改页面上的Javascript字符串链接无效,我也不知道为什么? - If a change a Javascript string links on a page don't work, and I don't know why? 我无法在JavaScript中使用“ \\ n”,我不知道为什么它不起作用 - I can't use '\n" in javascript, I don't know why it isn't work 我的机器人不和谐不起作用。 我不知道为什么 - My bot discord doesn't work. I don't know why 我的用户脚本不起作用,我也不知道为什么(过度使用var关键字) - My userscript doesn't work and I don't know why (overuse of var keyword) 我的javascript输出与预期输出不匹配。 我不知道哪里出错了 - My javascript output does not match the expected output. I don't know where I went wrong CSS 溢出-y:自动不起作用,但我不知道为什么 - CSS overflow-y: auto does not work, but I don't know why 我不知道为什么渲染在反应中不起作用 - I don't know why rendering doesn't work in react HTML和JavaScript,JavaScript没有输出,我不知道为什么? - HTML and JavaScript, no output from javascript, i don't know why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM