Ozkan
Ozkan

Reputation: 4170

Get current location of a visitor

I already found functionality that can give a users country or state using his or her IP address. But what I actually need is the exact location.

Like you can see on this link, it is possible to get the very specific current location of a user.

How is this possible with ASP.NET C#, Javascript or jQuery?

Upvotes: 0

Views: 3857

Answers (2)

Rab
Rab

Reputation: 35582

  navigator.geolocation.getCurrentPosition(
      onSuccess,
      onError, {
        enableHighAccuracy: true,
        timeout: 20000,
        maximumAge: 120000
      });

function onSuccess(position) {
      //the following are available to use
  //position.coords.latitude
  //position.coords.longitude
 // position.coords.altitude
 // position.coords.accuracy
 // position.coords.altitudeAccuracy
 // position.coords.heading
 // position.coords.speed 


}

you can find details here

Warning:This will popup a permission dialog in browsers that will look something like this (Safari): enter image description here

Upvotes: 5

Manse
Manse

Reputation: 38147

You could use the Geolocation API in JavaScript ... to get the current position use this :

navigator.geolocation.getCurrentPosition(function(position) {
  do_something(position.coords.latitude, position.coords.longitude);
});

do_something() could be to update a map or just show the current longitude and latitude. Nice simple example here

Browser support (Source):

  • Firefox 3.5+
  • Chrome 5.0+
  • Safari 5.0+
  • Opera 10.60+
  • Internet Explorer 9.0+

API Spec here

Upvotes: 5

Related Questions