谷歌浏览器如何知道我目前的GPS位置以及如何在我的代码中使用它? [英] How Google Chrome knows my current GPS location and how to use it in my code?

查看:532
本文介绍了谷歌浏览器如何知道我目前的GPS位置以及如何在我的代码中使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我意识到有些网页可以使用google chrome以非常精确的方式找到我(即时通过使用没有内置gps的笔记本电脑),我知道使用我的IP可以找到我,但不能用这种presision 。现在在谷歌地图中,您会看到一个按钮,位于您正在使用的黄色男士(街景)的顶部 google chrome



这是如何工作的?有一个 API 可以使用它吗?

解决方案

实际的HTML 5地理定位功能(仅当您的浏览器支持时):

  if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(success ,错误);
} else {
alert(Not Supported!);
}

函数成功(位置){
console.log(position.coords.latitude);
console.log(position.coords.longitude);


函数错误(msg){
console.log(typeof msg =='string'?msg:error);


watchId = navigator.geolocation.watchPosition(function(position){
console.log(position.coords.latitude);
console.log position.coords.longitude);
});

navigator.geolocation.clearWatch(watchId);

问候,
Sascha


today i realize that some pages can locate me (im using a laptop with no gps built-in) using google chrome with a an very impresive exactitude, i know that using my IP you can locate me, but not with such presision. Now in google maps you will see a button on top of the yellow men (street view) who locates you using google chrome.

How does this work? There is an API to use that?

解决方案

It's also possible with the actual HTML 5 Geolocation functions (only if your browser supports):

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(success, error);
} else {
  alert("Not Supported!");
}

function success(position) {
  console.log(position.coords.latitude);
  console.log(position.coords.longitude);
}

function error(msg) {
  console.log(typeof msg == 'string' ? msg : "error");
}

var watchId = navigator.geolocation.watchPosition(function(position) {  
  console.log(position.coords.latitude);
  console.log(position.coords.longitude);
});

navigator.geolocation.clearWatch(watchId);

Greetings, Sascha

这篇关于谷歌浏览器如何知道我目前的GPS位置以及如何在我的代码中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆