javascript - 地理定位仅适用于 Chrome

标签 javascript google-chrome opera

我试图从导航地理位置获取纬度和经度,但该代码仅适用于 Chrome...我在 Opera 中尝试过,但未显示警报 这是我的页面:https://codepen.io/Juan1417/pen/zKZkxy

$(document).ready(function() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      alert("latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude);
    });
  }
});
span {
  font-weight: bold;
  color: blue;
}

body {
  background-color: white;
  padding: 30px;
}

.img-center {
  margin: 0 auto;
  border-color: black;
  border-width: 4px;
  border-style: double;
  border-radius: 30px;
}

.title1 {
  font-family: Lobster;
  color: red;
}

.title2 {
  font-family: Amatic SC;
  color: green;
}

#d11 {
  border-style: outset;
  border-width: 10px 
}

#d112 {
  border-style: inset;
}
<link href="https://fonts.googleapis.com/css?family=Lobster|Amatic+SC" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<div class="container-fluid">
  <div id="d11"class="well">
    <div id="d111" class="text-center">
      <h1 class="title1">Alexander Fleming</h1> 
      <h2 class="title2">10 facts about the man that started the Antibiotic Revolution</h2>
    </div>
    <div id="112" class="well  text-center">
      <img src="https://lanaveva.files.wordpress.com/2010/03/alexander_fleming.png"class="img-responsive center-block img-center">
      </img>
    <p><br><strong>Fleming working in his laboratory.</strong></p>
  </div>
  <ul>
    <li><p><span>Alexander Fleming Fact 1:</span>
      Alexander Fleming was born on August 6th 1881 and during the 19th century period in history when scientific discoveries were being made and innovations in mathematics, physics, biology, electricity and chemistry were moving very fast.
      </p>
    </li>
    <li><p><span>Alexander Fleming Fact 2:</span>
      After Alexander Fleming finished his education he worked in a shipping office until he was twenty years old.
      </p>
    </li>
    <li><p><span>Alexander Fleming Fact 3:</span>
      Tom, his older brother, had become a physician and would encourage his brother to follow the same path. 
      </p>
    </li>
    <li><p><span>Alexander Fleming Fact 4:</span>
      In 1903 Fleming began his medical training at St Mary’s Hospital Medical School located in Paddington. By 1906 he had qualified with a distinction on his MBBS degree. 
      </p>
    </li>
    <li><p><span>Alexander Fleming Fact 5:</span>
      Since 1900 Fleming had been in the Volunteer Force as a private in the London Scottish Regiment. While attending medical school he had joined the rifle club there and the club captain suggested he apply to the research department of St Mary’s.
      </p>
    </li>
    <li><p><span>Alexander Fleming Fact 6:</span>
      This he did and would become the assistant to Sir Almoroth Wright a bacteriologist and pioneer in immunology and vaccine therapy.
      </p>
    </li>
    <li><p><span>Alexander Fleming Fact 7:</span>
      When the First World War broke out he became a captain in the Royal Army Medical Corps working in the hospitals on the battlefields of the Western Front.
      </p>
    </li>
    <li><p><span>Alexander Fleming Fact 8:</span>
      Having been away from his laboratory during the summer month of August, upon his return he found his stack of staphylococci cultures that he had left stacked on a corner bench, showed one of the cultures had been contaminated with a fungus.Upon further examination the colonies of staphylococci directly around the fungus were gone but in other areas where there was no fungus were as normal.Having identified the moulds origins as coming from the Penicillium genus and he would eventually call the substance penicillin, in the meantime he had taken to calling it “mould juice”. He continued with his research and experiments to see what could be achieved with this new discovery.
      </p>
    </li>
    <li><p><span>Alexander Fleming Fact 9:</span>
      Alexander Fleming once said "When I woke up just after dawn on September 28, 1928, I certainly didn't plan to revolutionise all medicine by discovering the world's first antibiotic, or bacteria killer, but I suppose that was exactly what I did."
      </p>
    </li>
    <li><p><span>Alexander Fleming Fact 10</span>
      -On March the 11th Alexander Fleming dies of a heart attack at his home in London at the age of seventy three.</p>
    </li>
  </ul>
  <blockquote>"Penicilline cures, but wine makes people happy"
    <footer>Alexander Fleming</footer>
  </blockquote>
  <p>If you want more information about Alexander Fleming 
    <a target="_blank" href="https://es.wikipedia.org/wiki/Alexander_Fleming">click here.</a>
  </p>
</div>

最佳答案

与其他浏览器一样,Opera 需要用户授予使用地理定位服务的权限。

The first time you go to a website that requests geolocation information, Google Location Services' terms and conditions are presented. If you agree to them, the service is activated.

After that, every time a website requests geolocation information, Opera tells you, and gives you a choice: to send your location data, or not to send it.

您很可能没有批准访问地理位置的请求。如果是这种情况,则不会发生任何事情,因为您的 if (navigator.geolocation) 将计算为 false,因此内部代码永远不会运行。

要重新启用地理定位服务,请按照以下说明操作。

If you at first reject Google Location Services' terms and conditions, but later change your mind, go to Settings > Preferences > Advanced > Network, and check Enable geolocation.

这些说明可以在这里找到:http://help.opera.com/FreeBSD/11.10/en/geolocation.html

关于javascript - 地理定位仅适用于 Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40392931/

相关文章:

javascript - 如何将数据从 Vue 实例传递到嵌套组件

css - opera 对 div 的定位与其他浏览器不同

JavaScript - 如何获得 PHP 服务器的正确时间?

javascript - 防止 react-redux 在状态改变时重新渲染整个页面

javascript - ROR + 从文本字段中检测类名并将其替换为另一个

javascript - if 条件 block 记录一条消息,但 else block 中仍在设置变量

javascript - 检测是否从 javascript 启用了移动 chrome 的数据保存功能

javascript - 无法在 Chrome/Opera/Safari 中解析 XML

javascript - 使用XHR通过POST发送大量数据

javascript - context drawImage/canvas toDataURL 在 Opera Mobile 上明显滞后