2012-06-12 93 views
0

我已經找到可以爲用戶國家或州使用他或她的IP地址的功能。但我真正需要的是確切的位置。獲取訪問者的當前位置

就像你可以在this link上看到的那樣,可以獲得用戶的特定當前位置。

ASP.NET C#,Javascript或jQuery可能如何實現?

+0

選中此http://stackoverflow.com/questions/1535619/google-geolocation-api-library – V4Vendetta

回答

3
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

警告:這將彈出一個對話框允許在瀏覽器中,這將是這個樣子(Safari瀏覽器): enter image description here

5

你可以在JavaScript中使用Geolocation API ...獲取當前位置利用這樣的:

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

do_something()可以更新地圖或只顯示當前的經度和緯度。 Nice simple example here

瀏覽器支持(Source):

  • 的火狐3.5
  • 鉻5.0+
  • Safari 5.0及
  • 歌劇10.60+
  • 的Internet Explorer 9.0及更高版本

API Spec here

+1

如果可以的話,我會upvote兩次,因爲您不會將它稱爲HTML5! :-) –

+0

您是否知道Google如何在沒有GPS或其他功能的情況下了解用戶的確切位置? – Ozkan

+1

@Ozkan請看這裏http://en.wikipedia.org/wiki/W3C_Geolocation_API – ManseUK