Javascript 如何从WebURL获取地理位置 我创建了一个ASP.NET网站,并将其托管在服务器上 假设我的web访问URL是“www.xyz.com” 现在,我想要地理位置谁访问我的网站,并显示在谷歌地图上。或者我可以从访问此Web URL的位置获取纬度或经度
那么,这可能吗?使用。HTML地理定位API用于定位用户的位置。你可以得到纬度和经度!! 这是我的密码:Javascript 如何从WebURL获取地理位置 我创建了一个ASP.NET网站,并将其托管在服务器上 假设我的web访问URL是“www.xyz.com” 现在,我想要地理位置谁访问我的网站,并显示在谷歌地图上。或者我可以从访问此Web URL的位置获取纬度或经度,javascript,asp.net,google-maps,geolocation,latitude-longitude,Javascript,Asp.net,Google Maps,Geolocation,Latitude Longitude,那么,这可能吗?使用。HTML地理定位API用于定位用户的位置。你可以得到纬度和经度!! 这是我的密码: if (navigator.geolocation) { // support geolocation var success = function(position){ latitude = position.coords.latitude; longitude = position.coords.longitude; con
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.050757
编辑
您希望查看访问您网站的所有用户的地理位置。
我认为有两种方法。将google analytics api与google maps结合使用或使用ajax(5秒将新地理位置数据更新为html和您的数据库(mysql、dynamo、redis等))
尝试以下操作以获取用户的位置: 除非设置超时和错误,否则获取当前位置的请求可能永远不会返回
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代码:
受保护的无效页面加载(对象发送方,事件参数e)
{
字符串ipAddress=Request.ServerVariables[“HTTP_X_FORWARDED_FOR”];
if(string.IsNullOrEmpty(ipAddress))
{
ipAddress=Request.ServerVariables[“REMOTE_ADDR”];
}
字符串APIKey=“”;
字符串url=string.Format(“http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json”,APIKey,ipAddress);
使用(WebClient=newWebClient())
{
string json=client.DownloadString(url);
Location Location=newJavaScriptSerializer()。反序列化(json);
列表位置=新列表();
位置。添加(位置);
gridView1.DataSource=位置;
gridView1.DataBind();
}
}
公共类位置
{
公共字符串IPAddress{get;set;}
公共字符串CountryName{get;set;}
公共字符串纬度{get;set;}
公共字符串经度{get;set;}
}
首先从网站访问的位置获取用户的位置。如何获取用户的位置?这适用于单个用户。我想查看访问我的网站的所有用户的地理位置我想同时显示所有用户的地理位置,尝试存储在数组中,但无法测试它。那么,如何才能获得完美的结果?这对单个用户来说是可行的。我想查看使用谷歌地图分析api或使用ajax(5s更新新地理数据)访问我的网站的所有地理位置用户@Chetan Sanghani请查看此文档
<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>
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; }
}