7

I would like to know how geolocation works in a web browser like Google Chrome or Firefox?

I can access this web site http://html5demos.com/geo and when I allow the browser to send my location, it shows my current location.

But when I send that URL to my friends to test it, it doesn't work for all of them. How does geolocation work and how do I implement it to get the correct location?

alexmuller
  • 2,207
  • 3
  • 23
  • 36
Giffary
  • 3,060
  • 12
  • 50
  • 71
  • Do you want it to work on a website? What platform? Do you only want to show a user where he is? Or do you want to show different locations? – Steven Sep 03 '10 at 07:50
  • -1 for not using the search feature: http://stackoverflow.com/questions/2248404/about-geolocation-in-html-5 – BeRecursive Sep 03 '10 at 07:53
  • 5 years later and generally you need to "allow" this. Of course it is not always accurate, and does not reveal "your" location, but the location of the host of the browser. So VNC/RDP/SSH (w/X) will put some distance between you and your "revealed" location. Proxy alone may not any more if your browser's host has GPS capabilities. A smartphone on the same subnet (ie. public and private ips can be used in back end search patterns) may betray you. – mckenzm Jul 24 '15 at 20:46

4 Answers4

6

When you visit a location-aware website, Firefox will ask you if you want to share your location.

If you consent, Firefox gathers information about nearby wireless access points and your computer’s IP address. Then Firefox sends this information to the default geolocation service provider, Google Location Services, to get an estimate of your location. That location estimate is then shared with the requesting website.

http://www.mozilla.com/en-US/firefox/geolocation/

So it depends on the browser they are using and some luck :-)

BarsMonster
  • 6,483
  • 2
  • 34
  • 47
  • I am logged in from PC, connected to internet using a proxy in my university. How does it tells my almost exact location while there is no gps thing with my pc? – SMUsamaShah Oct 03 '11 at 10:33
  • @LifeH2O http://security.stackexchange.com/questions/16253/how-does-google-maps-know-where-i-am-when-im-using-a-vpn – Augustin Riedinger Jun 02 '14 at 15:40
2

Re how it works: the Wikipedia article discusses several techniques (IP address location, cellphone and WiFi triangulation, GPS).

The HTML5 implementations require both browser support (FF 3.6, Opera 10.60, Chrome 4? 5?, IE maybe some day) and user consent before the geolocation data are retrieved.

As to how to implement it, the code of the demo you link to seems to be under the MIT License which basically says "you can do whatever, as long as you keep the resulting code under the license"; so you could take that code as a base to build on.

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
  • I am logged in from PC, connected to internet using a proxy in my university. How does it tells my almost exact location while there is no gps thing with my pc? – SMUsamaShah Oct 03 '11 at 10:33
  • 1
    LifeH2O: To quote myself: "the Wikipedia article discusses several techniques (**IP address location**,)" - perhaps the proxy is physically close to you? How "almost exact" is this? The accuracy depends - at university, "my" location was always some 100 miles off, since that was the location of the proxy (yeah, not optimal network design; but they did have *that* location correct down to the city block). – Piskvor left the building Oct 03 '11 at 10:59
1

Info and examples from W3C Geolocation API Specification:

The Geolocation API defines a high-level interface to location information associated only with the device hosting the implementation, such as latitude and longitude. The API itself is agnostic of the underlying location information sources. Common sources of location information include Global Positioning System (GPS) and location inferred from network signals such as IP address, RFID, WiFi and Bluetooth MAC addresses, and GSM/CDMA cell IDs, as well as user input. No guarantee is given that the API returns the device's actual location.

You could implement it this way:

(one-shot position request)

function showMap(position) {
  // Show a map centered at (position.coords.latitude, position.coords.longitude).
}

// One-shot position request.
navigator.geolocation.getCurrentPosition(showMap);

(requesting repeated position updates)

function scrollMap(position) {
  // Scrolls the map so that it is centered at (position.coords.latitude, position.coords.longitude).
}

// Request repeated updates.
var watchId = navigator.geolocation.watchPosition(scrollMap);

function buttonClickHandler() {
  // Cancel the updates when the user clicks a button.
  navigator.geolocation.clearWatch(watchId);
}
Agustín
  • 1,569
  • 1
  • 14
  • 9
0

Things really depend on the implementation of the website you are using and your browser.

Simplest way, which doesnt require any client side extensions is that the webpage gets your ip address and uses any of the "ip to geolocation" services to make a questimate where you are currently.

Second options is that browsers have an extension that can advertise your your coordinates to the webserver. In these cases, this information in the client side can be either fetched, again from ip to geolocation services or gps unit attached to your computer. For example, Nokia N900 and build-in browser MicroB has this sort of extension.

rasjani
  • 7,372
  • 4
  • 22
  • 35