"www.xyz.com"
。Geo Location
访问我的网站并将其显示在Google地图上。或者我可以从此网址访问的位置获取latitude
或longitude
吗?那么,这可能吗?
答案 0 :(得分:2)
使用HTML Geolocation。 HTML Geolocation 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 analytics api和谷歌地图或使用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 !');
}
答案 1 :(得分:1)
尝试这样来获取用户的位置: 除非您设置超时和错误,否则您获取当前位置的请求可能永远不会返回。
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);
};
答案 2 :(得分:1)
这是HTML CODE
<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; }
}