如何从 WebURL 获取地理位置
How to get Geo location from the WebURL
- 我已经创建了一个 ASP.NET 网站并将其托管在服务器上。
- 假设我的网络访问 URL 是
"www.xyz.com"
。
- 现在我想要
Geo Location
访问我的网站并将其显示在 Google 地图上。或者我可以从访问此 Web-URL 的地方获取 latitude
或 longitude
吗?
那么,这可能吗?
使用HTML Geolocation。 HTML 地理定位API 用于定位用户的位置。你可以获得纬度&&经度!
这是我的代码:
if (navigator.geolocation) {
// support geolocation
var success = function(position){
latitude = position.coords.latitude;
longitude = position.coords.longitude;
console.log('here latitude :'+latitude+' && longitude :'+longitude);
}
var errors = function(){
console.log('not working');
}
navigator.geolocation.getCurrentPosition(success, errors);
} else {
alert('upgrade your browser !');
}
我的输出是:此处纬度:37.5030042 && 经度:127.0507571
编辑
您想查看访问您网站的所有用户的地理位置。
我认为有两种方式。使用 google 分析 api 和 google 地图或使用 ajax(5 秒更新新的地理定位数据到 html& 你的数据库(mysql,dynamo, redis等..))
if (navigator.geolocation) {
// support geolocation
var success = function(position){
latitude = position.coords.latitude;
longitude = position.coords.longitude;
console.log('here latitude :'+latitude+' && longitude :'+longitude);
setInterval(function(e){
$.ajax({
type: "get",
dataType: 'json',
url: 'here request url',
cache: false,
data: { // request user geo data
latitude : latitude,
longitude : longitude
}
}).done(function(xhr) {
if(xhr.status){
// response users geo data and update your html DOM
$('#maps').html(xhr.geo.datas)
}else{
alert(xhr.msg);
}
})
},5000); // default 5 second
}
var errors = function(){
console.log('not working');
}
navigator.geolocation.getCurrentPosition(success, errors);
} else {
alert('upgrade your browser !');
}
尝试这样获取用户的位置:
除非您设置超时和错误,否则您获取当前位置的请求可能永远不会 return.
window.onload = function() {
var startPos;
var geoOptions = {
timeout: 10 * 1000
}
var geoSuccess = function(position) {
startPos = position;
document.getElementById('startLat').innerHTML = startPos.coords.latitude;
document.getElementById('startLon').innerHTML = startPos.coords.longitude;
};
var geoError = function(error) {
console.log('Error occurred. Error code: ' + error.code);
// error.code can be:
// 0: unknown error
// 1: permission denied
// 2: position unavailable (error response from location provider)
// 3: timed out
};
navigator.geolocation.getCurrentPosition(geoSuccess, geoError, geoOptions);
};
这是HTML代码
<asp:GridView ID="gridView1" runat="server" AutoGenerateColumns = "false">
<Columns>
<asp:BoundField DataField="IPAddress" HeaderText="IP Address" />
<asp:BoundField DataField="CountryName" HeaderText="Country" />
<asp:BoundField DataField="Latitude" HeaderText="Latitude" />
<asp:BoundField DataField="Longitude" HeaderText="Latitude" />
</Columns>
</asp:GridView>
这是 C# 代码:
protected void Page_Load(object sender, EventArgs e)
{
string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ipAddress))
{
ipAddress = Request.ServerVariables["REMOTE_ADDR"];
}
string APIKey = "<Your API Key>";
string url = string.Format("http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", APIKey, ipAddress);
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
Location location = new JavaScriptSerializer().Deserialize<Location>(json);
List<Location> locations = new List<Location>();
locations.Add(location);
gridView1.DataSource = locations;
gridView1.DataBind();
}
}
public class Location
{
public string IPAddress { get; set; }
public string CountryName { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
}
- 我已经创建了一个 ASP.NET 网站并将其托管在服务器上。
- 假设我的网络访问 URL 是
"www.xyz.com"
。 - 现在我想要
Geo Location
访问我的网站并将其显示在 Google 地图上。或者我可以从访问此 Web-URL 的地方获取latitude
或longitude
吗?
那么,这可能吗?
使用HTML Geolocation。 HTML 地理定位API 用于定位用户的位置。你可以获得纬度&&经度! 这是我的代码:
if (navigator.geolocation) {
// support geolocation
var success = function(position){
latitude = position.coords.latitude;
longitude = position.coords.longitude;
console.log('here latitude :'+latitude+' && longitude :'+longitude);
}
var errors = function(){
console.log('not working');
}
navigator.geolocation.getCurrentPosition(success, errors);
} else {
alert('upgrade your browser !');
}
我的输出是:此处纬度:37.5030042 && 经度:127.0507571
编辑
您想查看访问您网站的所有用户的地理位置。 我认为有两种方式。使用 google 分析 api 和 google 地图或使用 ajax(5 秒更新新的地理定位数据到 html& 你的数据库(mysql,dynamo, redis等..))
if (navigator.geolocation) {
// support geolocation
var success = function(position){
latitude = position.coords.latitude;
longitude = position.coords.longitude;
console.log('here latitude :'+latitude+' && longitude :'+longitude);
setInterval(function(e){
$.ajax({
type: "get",
dataType: 'json',
url: 'here request url',
cache: false,
data: { // request user geo data
latitude : latitude,
longitude : longitude
}
}).done(function(xhr) {
if(xhr.status){
// response users geo data and update your html DOM
$('#maps').html(xhr.geo.datas)
}else{
alert(xhr.msg);
}
})
},5000); // default 5 second
}
var errors = function(){
console.log('not working');
}
navigator.geolocation.getCurrentPosition(success, errors);
} else {
alert('upgrade your browser !');
}
尝试这样获取用户的位置: 除非您设置超时和错误,否则您获取当前位置的请求可能永远不会 return.
window.onload = function() {
var startPos;
var geoOptions = {
timeout: 10 * 1000
}
var geoSuccess = function(position) {
startPos = position;
document.getElementById('startLat').innerHTML = startPos.coords.latitude;
document.getElementById('startLon').innerHTML = startPos.coords.longitude;
};
var geoError = function(error) {
console.log('Error occurred. Error code: ' + error.code);
// error.code can be:
// 0: unknown error
// 1: permission denied
// 2: position unavailable (error response from location provider)
// 3: timed out
};
navigator.geolocation.getCurrentPosition(geoSuccess, geoError, geoOptions);
};
这是HTML代码
<asp:GridView ID="gridView1" runat="server" AutoGenerateColumns = "false">
<Columns>
<asp:BoundField DataField="IPAddress" HeaderText="IP Address" />
<asp:BoundField DataField="CountryName" HeaderText="Country" />
<asp:BoundField DataField="Latitude" HeaderText="Latitude" />
<asp:BoundField DataField="Longitude" HeaderText="Latitude" />
</Columns>
</asp:GridView>
这是 C# 代码:
protected void Page_Load(object sender, EventArgs e)
{
string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ipAddress))
{
ipAddress = Request.ServerVariables["REMOTE_ADDR"];
}
string APIKey = "<Your API Key>";
string url = string.Format("http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", APIKey, ipAddress);
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
Location location = new JavaScriptSerializer().Deserialize<Location>(json);
List<Location> locations = new List<Location>();
locations.Add(location);
gridView1.DataSource = locations;
gridView1.DataBind();
}
}
public class Location
{
public string IPAddress { get; set; }
public string CountryName { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
}